當(dāng)我使用routes屬性定義命名路由乒躺,并通過(guò)下列方法進(jìn)行攜帶arguments參數(shù)進(jìn)行跳轉(zhuǎn)
Navigator.pushNamed(
context,
routeName,
arguments: <args>,
);
然而,當(dāng)我在initState中通過(guò)下列方式接收參數(shù)時(shí),debug console中出現(xiàn)了報(bào)錯(cuò)
@override
void initState() {
args = ModalRoute.of(context).settings.arguments;
}
報(bào)錯(cuò)內(nèi)容如下
20:49:44.129 4 info flutter.tools I/flutter ( 2680): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
20:49:44.129 5 info flutter.tools I/flutter ( 2680): The following assertion was thrown building Builder:
20:49:44.129 6 info flutter.tools I/flutter ( 2680): inheritFromWidgetOfExactType(_ModalScopeStatus) or inheritFromElement() was called before
20:49:44.130 7 info flutter.tools I/flutter ( 2680): _CourseCohortScreenState.initState() completed.
20:49:44.130 8 info flutter.tools I/flutter ( 2680): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent
20:49:44.131 9 info flutter.tools I/flutter ( 2680): widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor
20:49:44.131 10 info flutter.tools I/flutter ( 2680): or an initState() method, then the rebuilt dependent widget will not reflect the changes in the
20:49:44.131 11 info flutter.tools I/flutter ( 2680): inherited widget.
20:49:44.138 12 info flutter.tools I/flutter ( 2680): Typically references to inherited widgets should occur in widget build() methods. Alternatively,
20:49:44.138 13 info flutter.tools I/flutter ( 2680): initialization based on inherited widgets can be placed in the didChangeDependencies method, which
20:49:44.138 14 info flutter.tools I/flutter ( 2680): is called after initState and whenever the dependencies change thereafter.
后來(lái)google之后找到了解決辦法(雖然目前的狀態(tài)還是知其然不知其所以然)
- 配置落地頁(yè)的構(gòu)建和參數(shù)調(diào)用邏輯:
// login_page.dart
class LoginPage extends StatefulWidget {
final String share_id;
LoginPage({ Key key, this.share_id }) : super(key: key);
@override
State<StatefulWidget> createState() => LoginPageState();
}
class LoginPageState extends State<LoginPage> {
@override
void initState() {
super.initState();
getUserInfo(widget.user_id); // 使用userId
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Text(
'hello ${widget.user_id}',
textScaleFactor: 5.0,
),
),
);
}
}
- 在MaterialApp中使用onGenerateRoute屬性來(lái)定義命名路由堪旧,在需要傳入?yún)?shù)的Widget頁(yè)面定義初始化變量削葱,并在路由定義時(shí)傳入變量:
// main.dart
import './pages/login_page.dart' // 引用LoginPage
...
onGenerateRoute: (RouteSettings settings) {
print('build route for ${settings.name}');
var routes = <String, WidgetBuilder>{
"login": (ctx) => LoginPage(settings.arguments),
"other": (ctx) => SomeWidget(),
};
WidgetBuilder builder = routes[settings.name];
return MaterialPageRoute(builder: (ctx) => builder(ctx));
},
3此時(shí)便可以調(diào)用相同的方法來(lái)實(shí)現(xiàn)路由跳轉(zhuǎn),并傳入?yún)?shù)
Navigator.of(context).pushNamed("login", arguments: "IDXXXX");