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

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

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.

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:

确定