'How to move fast after fetching data from api on pressing button Flutter

I want to ask if there is a future function who gives data from api and i want to get that data when i pressed the button and i want some particular data like id of user i am using function like

onPressed: () async {
     var data = await getLogin()
     if(data.id == 0){
       Navigator.push(context,
                      MaterialPageRoute(builder: ((context) => Login())));
     else {
        Navigator.push(context,
                      MaterialPageRoute(builder: ((context) => Order())));
      }
}

Can you tell me why it is taking time when i am pressing button to open new screen and can any one tell me the solution



Solution 1:[1]

it is taking time because it await for data. It is a async function and this await keyword wait until you didn't get the data back..

Problem is that if you not use await keyword then data.id throw a null safety error....

So solution is Future Provider... Call the api using the any state management solution like provider and navigate without checking the condition... Then update Ui after getting data from Provider..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vishal_VE