In my main.dart
file I have a Future
that fetches data from an API (json
file).
What is the proper way that I can navigate to a new screen as soon as Future
finishes fetching data?
In my main.dart
file I have a Future
that fetches data from an API (json
file).
What is the proper way that I can navigate to a new screen as soon as Future
finishes fetching data?
You can do it like this:
yourFutureObject.then((){
//write the code you want to run as soon as your Future finishes
Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) =>
YourNewPage()));
});
I used some general names because you didn't post any code but you can write the function you want to run when Future finished and pass it to Future’s then method
So you have a Method that returns a Future
bool asyncResult2 = await asyncFunc2();
if(asyncResult2)
{
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
}
asyncFunc2 funtion when completes the data fetching and assigning (or anything else) then all of these is done you can just pass the Future of boolean value true which states that every thing was done sucessfully and you can proceed to the second page or else if the the boolean value is false then there was something wrong with fetching or assigning