Last active
March 30, 2020 10:38
-
-
Save nashfive/221d42f266b1a4590b44bab322bb8afb to your computer and use it in GitHub Desktop.
Provider error with ChangeNotifierProvider and Navigator.popUntil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() => runApp(MyApp()); | |
class Store extends ChangeNotifier { | |
final createdAt = DateTime.now(); | |
dispose() { | |
print("store.dispose()"); | |
super.dispose(); | |
} | |
Store() { | |
print(toString()); | |
} | |
@override | |
String toString() { | |
return 'Store{createdAt: $createdAt}'; | |
} | |
@override | |
bool operator ==(Object other) => | |
identical(this, other) || | |
other is Store && | |
runtimeType == other.runtimeType && | |
createdAt == other.createdAt; | |
@override | |
int get hashCode => createdAt.hashCode; | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Home(), | |
routes: { | |
'feature': (_) => ChangeNotifierProvider( | |
create: (_) => Store(), | |
child: Feature(), | |
), | |
}, | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: RaisedButton( | |
child: Text("Feature"), | |
onPressed: () => Navigator.of(context).pushNamed('feature'), | |
), | |
), | |
); | |
} | |
} | |
class Feature extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final store = Provider.of<Store>(context, listen: false); | |
return Scaffold( | |
appBar: AppBar(title: Text("FEATURE")), | |
body: Center( | |
child: RaisedButton( | |
child: Text('Next'), | |
onPressed: () { | |
Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (_) => ChangeNotifierProvider.value( | |
value: store, | |
child: Confirm(), | |
), | |
), | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class Confirm extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final store = Provider.of<Store>(context, listen: false); | |
return Scaffold( | |
appBar: AppBar(title: Text("CONFIRM")), | |
body: Center( | |
child: RaisedButton( | |
child: Text('Next'), | |
onPressed: () { | |
Navigator.of(context).push( | |
MaterialPageRoute( | |
builder: (_) => ChangeNotifierProvider.value( | |
value: store, | |
child: Finish(), | |
), | |
), | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class Finish extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return WillPopScope( | |
onWillPop: () async => false, | |
child: Scaffold( | |
body: Center( | |
child: RaisedButton( | |
child: Text("Close"), | |
onPressed: () { | |
Navigator.of(context).popUntil(ModalRoute.withName('/')); | |
}, | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First run is fine, second (and following) run has an exception: