Last active
February 5, 2020 20:33
-
-
Save digitalkaoz/30e8ede34cb1bac9f1f7f911cdb4567e to your computer and use it in GitHub Desktop.
Flutter mobx state problem
This file contains 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
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(); | |
} | |
} |
christianhaller3000
commented
Feb 5, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment