I'm new to Flutter and Dart. So I'm trying to build a simple authentication system in Flutter. When the User opens the app, I want to either show them the LoginScreen (where they had not previously logged in) or the HomeScreen when they are logged in.
loadWidget, which returns a Future would determine whether there is persistent data and hence fetches the user's info and displays the Home Screen. The child property would not let me assign a Future to it, I'm not sure but I suppose I would have to use FutureBuilder widget for this?
First Try
Future<Widget> loadWidget() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
// already logged in
String phone = prefs.getString('phoneNumber');
if (phone != null) {
helper.user = await checkUser(phone); // fetch user info
return HomeScreen();
}
// Not logged in
else {
return Login();
}
}
For the build function, this does not work
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: loadWidget(), // This does not allow me to assign a future
)
);
}
Second Try: Using the FutureBuilder
I read about Futurebuilder here how to assign future<> to widget in flutter? and it looks like the way to go, so I modified my build method as
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: FutureBuilder(
future: loadWidget(),
builder: (BuildContext context, AsyncSnapshot<Widget> widget){
return widget;
},
),
),
);
}
But this gave me the following error:
The return type 'AsyncSnapshot' isn't a 'Widget', as defined by anonymous closure.
How can I accomplish this? Thanks!