从initialRoute弹出时重新加载小部件

时间:2020-06-17 04:14:21

标签: flutter

我刚刚开始学习入门知识,并希望实现登录页面。

因此,我检查是否有令牌,然后决定将其初始化为@Override public boolean equals(@Nullable Object thatObject) { if (!(thatObject instanceof Entry)) { return false; } Entry<?,?> thatEntry = (Entry<?,?>) thatObject; if (!Objects.equals(this.key, thatEntry.key)) { return false; } return Objects.equals(this.value, thatEntry.value)); } 是我的home route还是login route,问题是抖动渲染initialRoute ,它还会重新请求我在login route上声明的API,该API将返回home route,因为我还没有任何令牌。

empty list

先进行身份验证后,再从登录按钮使用Future<void> main() async{ WidgetsFlutterBinding.ensureInitialized(); SharedPreferences prefs = await SharedPreferences.getInstance(); var status = prefs.getString('jwt') ?? ""; if(status!="") runApp(HomeR(initialRoute: "/")); else runApp(HomeR(initialRoute: "/login")); } class HomeR extends StatelessWidget { static const routeName = '/'; HomeR({Key key, this.initialRoute}) : super(key: key); String initialRoute = "/"; // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'blablabal', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'blablabla'), initialRoute: initialRoute, routes: { loginR.routeName: (context) => loginR(), }, ); } } ,但是我的Navigator.pop(context)不会刷新自身,也不显示任何内容,因为它以前使用过home route

当我empty list时如何告诉home route重新加载?

1 个答案:

答案 0 :(得分:1)

这是MaterialApp

MaterialApp(
      // no need for home
      title: 'blablabal',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        '/': (context) => InitialApp(),
        loginR.routeName: (context) => loginR(),
      },
      initialRoute: '/',
),

和在InitialApp中

class InitialApp extends StatefulWidget {
  @override
  _InitialAppState createState() => _InitialAppState();
}

class _InitialAppState extends State<InitialApp> {
  void loginLogic() async
  {
    SharedPreferences.getInstance().then((value){
      var status = value.getString('jwt') ?? "";
      if(status=='')
      {
         Navigator.pushReplacementNamed(context, loginR.routeName);
      }
      else //logged in
      {
         Navigator.pushReplacementNamed(context, LaporanList.routeName);
      }
    });
  }

  @override
  void initState() {
    loginLogic();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}