0
I’m trying to run a check through the token:
If there is a token, display the page Home
and, if not, displays the page Login
.
I tried this way:
main.Dart:
class MyApp extends StatelessWidget {
// Create storage
final storage = new FlutterSecureStorage();
checkHome() async {
// Read value
String token = await storage.read(key: 'token');
if(token != null){
return HomePage();
}else{
return LoginPage();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cadê meu pet?',
theme: ThemeData(primarySwatch: Colors.yellow),
home: checkHome(),
);
}
}
However, I am getting:
type 'Future' is not a subtype of type 'Widget'
I cannot return to home/login through this function?
I also tried to:
void main() async {
// Set default home.
Widget _defaultHome = new LoginPage();
// Read value
final storage = new FlutterSecureStorage();
String token = await storage.read(key: 'token');
if (token != null) {
_defaultHome = new HomePage();
}
// Run app!
runApp(new MaterialApp(
title: 'Cadê meu pet?',
theme: ThemeData(primarySwatch: Colors.yellow),
home: _defaultHome,
));
}
His method
checkHome()
has the asynchronous assignment. This is because you have inserted theasync
, with that the return is something in the future (Future
as it is getting in error). Already the component that the flutter class is waiting for is of the typeWidget
and as its return is another type occurs error. One of the solutions to your problem would be the definition of a variable that receives the resolution of the future method and from it choose with a ternary which page you want to display.– Leonardo Paim
how can I receive the resolution of my method?
– veroneseComS