英文:
ModalRoute.of(context).settings.arguements always null
问题
导航到带有数据的详细屏幕
Navigator.of(context).pushNamed('/detailScreen', arguments: "data");
DetailScreen: 这里我从Modal Routes中获取到null。
class DetailScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final arguments = ModalRoute.of(context)!.settings.arguments;
print(arguments); // 总是null
return Scaffold(
body: Container(),
);
}
}
以下是main.dart中的应用程序代码
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
title: Strings.appName,
theme: CustomTheme.lightTheme,
onGenerateRoute: AppRoutes.generateRoute,
home: MainScreen(),
);
}
}
传递和接收参数。
英文:
navigation to detailscreen with data
Navigator.of(context).pushNamed('/detailScren', arguments: "data");
DetailScreen: here i'm getting null from Modal Routes.
class DetailScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final arguments = ModalRoute.of(context)!.settings.arguements;
print(arguments); // always null
return Scaffold(
body: Container(),
);
}
Below is App code in main.dart
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
title: Strings.appName,
theme: CustomTheme.lightTheme,
onGenerateRoute: AppRoutes.generateRoute,
home: MainScreen(),
);
}
Arguments passing and receiving.
答案1
得分: 1
onGenerateRoute
方法与直接将参数传递给路由不同。您应该在onGenerateRoute
内部直接管理参数:https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments#alternatively-extract-the-arguments-using-ongenerateroute
英文:
The onGenerateRoute
approach is different from passing arguments directly to the route. You should manage arguments directly inside onGenerateRoute
: https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments#alternatively-extract-the-arguments-using-ongenerateroute
答案2
得分: 0
你写道:
ModalRoute.of(context)!.settings.arguements;
这是一个拼写错误:应该是 arguments,而不是 arguements。
英文:
You wrote
ModalRoute.of(context)!.settings.arguements;
It's a typo: arguements, instead of arguments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论