异步方法返回响应后如何导航不同的页面

时间:2019-01-16 17:06:38

标签: dart flutter

我有一个屏幕应用程序,其中我具有MaterialApp的onGenerateRoute属性。在路由方法中,我进行了api调用,一旦获得响应,我想让用户导航到登录屏幕

我尝试在.then()函数中调用我的小部件Login

class App extends StatelessWidget {
  Widget build(BuildContext context) {
    return AppBlocProvider(
      child: LoginBlocProvider(
        child: MaterialApp(
          onGenerateRoute: routes,
        ),
      ),
    );
  }

  Route routes(RouteSettings settings) {
    print(settings.name);
    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (context) {
          //HERE I AM MAKING API CALL
          final appBloc = AppBlocProvider.of(context);
          appBloc.verifyUser().then((response) {
            //HERE ONCE I GET THE RESPONSE I WANT TO NAVIGATE USER TO 
            //lOGIN ACTIVITY
            print('called');
            return Login();
          });
          return AppBlocProvider(
            child: Center(child: CircularProgressIndicator()),
          );
        });
        break;
      case '/Login':
        return MaterialPageRoute(builder: (BuildContext context) {
          return Login();
        });
        break;
      case '/HomeScreen':
        return MaterialPageRoute(builder: (BuildContext context) {
          return Home();
        });
        break;
    }
    return MaterialPageRoute(builder: (context) {
      print('returned null');
    });
  }

api调用成功,甚至.then()方法执行,但没有出现登录屏幕

0 个答案:

没有答案