Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning data from .pop() to use it anywhere

I have written a code that return bool type variables. Whether you like the movie. If you like the movie then it returns true, if you don’t like the movie then it returns false. But since the .pop() method works in ElevatedButton, I cannot reach it from another class. How can I reach the value?

ElevatedButton(
          child: Text(
              "Go to new page"
          ),
          onPressed: () async {
            final answer = await Navigator.of(context).push<bool>(
                MaterialPageRoute(
                  builder: (context) {
                    return VideoScreen("Did you like the video?");
                },
              )
            );
          },
        ),

However, I cannot say like:

    ElevatedButton(
      child: Text(
          "Go to new page"
      ),
      onPressed: () async {
        final answer = await Navigator.of(context).push<bool>(
            MaterialPageRoute(
              builder: (context) {
                return VideoScreen("Did you like the video?");
            },
          )
        );
      },
    ),
    Text(answer)
  ],
);

So how can I reach that value? Callback or something? Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use can pass value to parent route while pop like

Navigator.of(context).pop(YourValue);

While you push make sure to await.

final result =  await Navigator.of(context)....;
print(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading