页面中 Navigator 跳转,报错了
来源:2-1 Android开发者如何快速上手Flutter开发

非同凡想之人
2022-05-02
Widget _buildBody() {
if (Global.profile.accessToken == null) {
Navigator.of(context).pushNamed('/log-in');
}
return const Center(
child: Text('No memory found...'),
);
}
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: UserModel()),
ChangeNotifierProvider.value(value: LocaleModel())
],
child: Consumer2<UserModel, LocaleModel>(
builder: (BuildContext context, userModel, localeModel, child) {
return Scaffold(
appBar: _buildAppBar(userModel),
body: _buildBody(),
);
},
),
);
}
老师你看,我其实是想进入页面的时候判断是否有 access_token,如果没有我要跳转到登录页。
但是每次启动的时候都会报错:
‘package:flutter/src/widgets/navigator.dart’: Failed assertion: line 5127 pos 12: ‘!_debugLocked’: is not true.
或者
======== Exception caught by widgets library =======================================================
The following assertion was thrown building Consumer2<UserModel, LocaleModel>(dirty, dependencies: [_InheritedProviderScope<LocaleModel?>, _InheritedProviderScope<UserModel?>]):
setState() or markNeedsBuild() called during build.
不知道是什么原因,还请教教我怎么做才是对的?谢谢!
写回答
1回答
-
CrazyCodeBoy
2022-05-02
改成在initState方法中监听值的变化然后做跳转判断试试看是否能解决这个问题呢00
相似问题