1

In this simple flutter GitHub repository implementation we have a simple screen which we want to pass arguments and getting them in destination screen, for example

form ScreenA() we want pass arguments to ScreenB()

defined routes:

routes: {
  '/page/one': (context) => ScreenA(),
  '/page/two': (context) => ScreenB(),
  '/page/three': (context) => ScreenC(),
},

fist of all we have ScreenA():

class ScreenA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Navigation with params'),
      ),
      body: Center(
        child:  ElevatedButton(
          child: Text('Click me!'),
          onPressed: () {
            Navigator.pushNamed(
              context,
              '/page/two',
              arguments: PageArguments(
                  id: 1,
                  title: "Example Title"
              ),
            );
          },
        ),
      ),
    );
  }
}

and destination page as ScreenB() is:

class ScreenB extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final arguments = ModalRoute.of(context)!.settings.arguments;

    /* getting currect route name */
    print(ModalRoute.of(context)!.settings.name);

    /* getting NULL arguments */
    print(arguments);

    return Scaffold();
  }
}

class PageArguments{
  final int id;
  final String title;
  PageArguments({required this.id, required this.title});
}
2

2 Answers 2

3

It seems that Persistent Bottom Navigation Bar package does not pass correctly route information, it could be a bug. If you put a breakpoint in the build method of ScreenB, you will see that not only the argument is null, but route name is also wrong.

I tried in main.dart with onGenerateRoute and it worked for me this way:


  List<PersistentBottomNavBarItem> _navBarsItems() {
    return [
      PersistentBottomNavBarItem(
          title: 'Home',
          icon: Icon(Icons.home),
          routeAndNavigatorSettings: RouteAndNavigatorSettings(
              initialRoute: '/page/one',
              onGenerateRoute: (RouteSettings settings) {
                WidgetBuilder builder;
                // Manage your route names here
                switch (settings.name) {
                  case '/page/one':
                    builder = (BuildContext context) => ScreenA();
                    break;
                  case '/page/two':
                    builder = (BuildContext context) => ScreenB();
                    break;
                  default:
                    throw Exception('Invalid route: ${settings.name}');
                }
                return MaterialPageRoute(
                  builder: builder,
                  settings: settings,
                );
              })),
      PersistentBottomNavBarItem(title: 'Help', icon: Icon(Icons.help)),
    ];
  }

About casting the argument, I agree with the other answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome. About the bug (if it is a bug) you can contact the package author.
0

Main and MyApp for reference

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ScreenA(),
      routes: {
        '/page/one': (context) => ScreenA(),
        '/page/two': (context) => ScreenB(),
      },
    );
  }
}

Update ClassB with below code no modification required in ClassA

class ScreenB extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final arguments =
        ModalRoute.of(context)!.settings.arguments as PageArguments;

    print(ModalRoute.of(context)!.settings.name);
    print(arguments.id);
    print(arguments.title);
    print("------");

    return Scaffold();
  }
}

Key update:

Typecasting the argument.

final arguments =
    ModalRoute.of(context)!.settings.arguments as PageArguments;

Console log:

enter image description here

2 Comments

It works with normal route definition, but not with persistent_bottom_nav_bar package.
I see since no reference to this package in question I was not aware of it. Thanks @PeterKoltai

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.