Skip to content

Instantly share code, notes, and snippets.

@digitalkaoz
Last active February 5, 2020 20:33
Show Gist options
  • Save digitalkaoz/30e8ede34cb1bac9f1f7f911cdb4567e to your computer and use it in GitHub Desktop.
Save digitalkaoz/30e8ede34cb1bac9f1f7f911cdb4567e to your computer and use it in GitHub Desktop.
Flutter mobx state problem
final List<InheritedProvider> providers = [
Provider<Api>(create: (_) => Api()),
Provider<X>(create: (_) => X()),
Provider<Y>(create: (_) => Y()),
ProxyProvider3<Api, X, Y, AppData>(
update: (_, api, x, y, __) => AppData(api, x, y),
),
];
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: providers,
child: MaterialApp(
title: 'App',
routes: {
'LOADING': (_) => LoadingPage(),
'INDEX': (_) => IndexPage(),
},
initialRoute: 'LOADING',
),
);
}
}
class LoadingPage extends StatelessWidget {
Future<void> _load(BuildContext context, AppData appData) async {
await appData.loadInitialData(); # initialize foo
}
@override
Widget build(BuildContext context) {
final appData = Provider.of<AppData>(context);
return Material(
child: FutureBuilder(
future: _load(context, appData),
builder: (BuildContext _, AsyncSnapshot future) {
if (future.connectionState != ConnectionState.done) {
return Text("loading");
}
if (future.error != null) {
return Text(future.error.toString())
}
WidgetsBinding.instance.addPostFrameCallback((duration) =>
Navigator.of(context)
.pushReplacement(route(builder: (_) => IndexPage())));
return Container();
},
),
);
}
}
class IndexPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appData = Provider.of<AppData>(context);
print(appData.foo); #foo is initialized from the loading screen
return Container();
}
}
@digitalkaoz
Copy link
Author

initially the Flow work, the AppData gets populated from some remote stuff and the redirect to the real page works.

But when hot-reloading happens on the real page, the store is reset to its initial state, the question is why ?

@christianhaller3000
Copy link

Download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment