ModalRoute.of(context).settings.arguments 始终为null。

huangapple go评论148阅读模式
英文:

ModalRoute.of(context).settings.arguements always null

问题

导航到带有数据的详细屏幕

  1. Navigator.of(context).pushNamed('/detailScreen', arguments: "data");

DetailScreen: 这里我从Modal Routes中获取到null。

  1. class DetailScreen extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. final arguments = ModalRoute.of(context)!.settings.arguments;
  5. print(arguments); // 总是null
  6. return Scaffold(
  7. body: Container(),
  8. );
  9. }
  10. }

以下是main.dart中的应用程序代码

  1. class App extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. localizationsDelegates: context.localizationDelegates,
  6. supportedLocales: context.supportedLocales,
  7. locale: context.locale,
  8. title: Strings.appName,
  9. theme: CustomTheme.lightTheme,
  10. onGenerateRoute: AppRoutes.generateRoute,
  11. home: MainScreen(),
  12. );
  13. }
  14. }

传递和接收参数。

英文:

navigation to detailscreen with data
Navigator.of(context).pushNamed('/detailScren', arguments: "data");

DetailScreen: here i'm getting null from Modal Routes.

  1. class DetailScreen extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. final arguments = ModalRoute.of(context)!.settings.arguements;
  5. print(arguments); // always null
  6. return Scaffold(
  7. body: Container(),
  8. );
  9. }

Below is App code in main.dart

  1. class App extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. localizationsDelegates: context.localizationDelegates,
  6. supportedLocales: context.supportedLocales,
  7. locale: context.locale,
  8. title: Strings.appName,
  9. theme: CustomTheme.lightTheme,
  10. onGenerateRoute: AppRoutes.generateRoute,
  11. home: MainScreen(),
  12. );
  13. }

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

你写道:

  1. ModalRoute.of(context)!.settings.arguements;

这是一个拼写错误:应该是 arguments,而不是 arguements。

英文:

You wrote

  1. ModalRoute.of(context)!.settings.arguements;

It's a typo: arguements, instead of arguments.

huangapple
  • 本文由 发表于 2023年8月10日 18:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定