简体   繁体   中英

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

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 ?

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM