Home > Blockchain >  how to pass value of onTap() from a page to another dart/flutter
how to pass value of onTap() from a page to another dart/flutter

Time:10-21

im trying to store a value from onTap() into a variable to use it in another page

onTap: () {
                    getCourseName(doc["Course name"]);
                    Navigator.push(
                      context,
                      PageTransition(
                        type: PageTransitionType.rightToLeft,
                        child: XDSections(),
                      ),
                    );
                  },

this code was used in page Courses I want to use this value in a way to use it page Section

how do i do that ?

CodePudding user response:

You can pass the course name as an argument while navigating to you next screen.

Navigator.push(
    context,
    PageTransition(
        type: PageTransitionType.rightToLeft,
        child: XDSections(),
    ),
    arguments: {'courseName': "PASS_YOUR_VALUE_HERE"}
);

On your next screen you can get that value by following:

var arguments = ModalRoute.of(context).settings.arguments;

CodePudding user response:

You can use (arguments) parameter in Navigator.push() in order to pass data from one page to another. Example:

              Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => XDSections(),
                    settings: RouteSettings(
                      arguments: "Passed Data",
                    ),
                  ),
                );

and for receiving it on XDSections Build Widget:

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

More info on documentation

also it's answered here in more details.

  • Related