gpt4 book ai didi

Flutter:在 Widget 的状态的 initState 中获取 Navigator 传递的参数

转载 作者:IT王子 更新时间:2023-10-29 06:47:33 25 4
gpt4 key购买 nike

我有一个 StatefulWidget,我想在命名路由中使用它。我必须按照 https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments 中的建议传递一些我正在做的论点即

Navigator.pushNamed(
context,
routeName,
arguments: <args>,
);

现在,我需要在状态的 initState 方法中访问这些参数,因为需要参数来订阅一些外部事件。如果我将 args = ModalRoute.of(context).settings.arguments; 调用放在 initState 中,我会得到一个运行时异常。

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.
20:49:44.138 15 info flutter.tools I/flutter ( 2680):
20:49:44.138 16 info flutter.tools I/flutter ( 2680): When the exception was thrown, this was the stack:
20:49:44.147 17 info flutter.tools I/flutter ( 2680): #0 StatefulElement.inheritFromElement.<anonymous closure> (package:flutter/src/widgets/framework.dart:3936:9)
20:49:44.147 18 info flutter.tools I/flutter ( 2680): #1 StatefulElement.inheritFromElement (package:flutter/src/widgets/framework.dart:3969:6)
20:49:44.147 19 info flutter.tools I/flutter ( 2680): #2 Element.inheritFromWidgetOfExactType (package:flutter/src/widgets/framework.dart:3285:14)
20:49:44.147 20 info flutter.tools I/flutter ( 2680): #3 ModalRoute.of (package:flutter/src/widgets/routes.dart:698:46)
20:49:44.147 21 info flutter.tools I/flutter ( 2680): #4 _CourseCohortScreenState.initState.<anonymous closure> (package:esk2/cohort_screen.dart:57:23)

我不想将该逻辑放在 build 方法中,因为 build 可以被调用多次,而初始化只需要发生一次。我可以将整个逻辑放在一个带有 bool isInitialized 标志的 block 中,但这似乎不是正确的做法。 Flutter 目前是否不支持此要求/案例?

最佳答案

像这样使用 MaterialApp.onGenerateRoute 属性:

onGenerateRoute: (RouteSettings settings) {
print('build route for ${settings.name}');
var routes = <String, WidgetBuilder>{
"hello": (ctx) => Hello(settings.arguments),
"other": (ctx) => SomeWidget(),
};
WidgetBuilder builder = routes[settings.name];
return MaterialPageRoute(builder: (ctx) => builder(ctx));
},

现在您可以简单地使用 NavigatorState.pushNamed:

Navigator.of(context).pushNamed("hello", arguments: "world");

这里有一些测试 Hello 小部件:

class Hello extends StatelessWidget {
final String greet;

Hello(this.greet);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Text(
'hello $greet',
textScaleFactor: 5.0,
),
),
);
}
}

关于Flutter:在 Widget 的状态的 initState 中获取 Navigator 传递的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56262655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com