Masaya
Masaya

Reputation: 301

How to get return value with pop on Flutter?

I would like to know how to get two values with pop on Flutter. I tried to write the code. but I got the exception "A non-null String must be provided to a Text widget."

Here is the codes.

First Screen

                      ElevatedButton(
                        child: const Text('move to second screen'),
                        style: ElevatedButton.styleFrom(
                            primary: Colors.orange, onPrimary: Colors.white),
                        onPressed: () async {
                          final List<String> _response =
                              await Navigator.pushNamed(
                                  context, '/adminCategoryPicker');
                          emoji = _response[0];
                          emojiName = _response[1];
                        },
                      ),

Second Screen

return Card(
                  child: ListTile(
                    leading: Text(targetElectricsEmojiLists[index]),
                    title: Text(targetElectricsNameLists[index]),
                    onTap: () {
                      setState(() {
                        _selectedName = targetElectricsNameLists[index];
                        _selectedEmoji = targetElectricsEmojiLists[index];
                      });
                      Navigator.pop(
                        context,
                        {_selectedName, _selectedEmoji},
                      );
                    },
                  ),
                );

I think this issue is below section. Do you know other way to write the codes?

First Screen

onPressed: () async {
     final List<String> _response =
     await Navigator.pushNamed(
          context, '/goToSecondScreen');
           emoji = _response[0];
           emojiName = _response[1];
},

Second Screen

Navigator.pop(
       context,
       {_selectedName, _selectedEmoji},
);

I changed the codes. Moreover, I got the errors.

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'MaterialPageRoute' is not a subtype of type 'Route<List>?' in type cast

First Screen

ElevatedButton(
                        child: const Text('move to second screen'),
                        style: ElevatedButton.styleFrom(
                            primary: Colors.orange, onPrimary: Colors.white),
                        onPressed: () async {
                          final List<String> _response =
                              await Navigator.pushNamed(
                                  context, '/adminCategoryPicker');
                          emojiName = _response[0];
                          emoji = _response[1];
                        },
                      ),

Second Screen

return Card(
                  child: ListTile(
                    leading: Text(targetElectricsEmojiLists[index] ?? 'null'),
                    title: Text(targetElectricsNameLists[index] ?? 'null'),
                    onTap: () {
                      setState(() {
                        _selectedName = targetElectricsNameLists[index];
                        _selectedEmoji = targetElectricsEmojiLists[index];
                      });
                      Navigator.pop(
                        context,
                        [_selectedName, _selectedEmoji],
                      );
                    },
                  ),
                );

Upvotes: 3

Views: 8691

Answers (3)

Laxmikanth Madhyastha
Laxmikanth Madhyastha

Reputation: 342

You can try something like below

First Screen

                 ElevatedButton(
                    child: const Text('move to second screen'),
                    style: ElevatedButton.styleFrom(
                        primary: Colors.orange, onPrimary: Colors.white),
                    onPressed: () {
                       
                        Navigator.pushNamed(context,'/adminCategoryPicker')
                        .then((value) 
                          {
                             emoji = (value as Map)['emoji'];
                             emojiName =  (value as Map)['emojiName'];
                          });
                       },
                     ),

Second Screen

return Card(
                  child: ListTile(
                    leading: Text(targetElectricsEmojiLists[index]),
                    title: Text(targetElectricsNameLists[index]),
                    onTap: () {
                      setState(() {
                        _selectedName = targetElectricsNameLists[index];
                        _selectedEmoji = targetElectricsEmojiLists[index];
                      });
                      Navigator.pop(
                        context,{'emoji':_selectedName,'emojiName':_selectedEmoji}
                      );
                    },
                  ),
                );

Upvotes: 6

Ricky
Ricky

Reputation: 46

First Screen

onPressed: () async {
     final dynamic _response =
     await Navigator.pushNamed(
          context, '/goToSecondScreen');
           emoji = _response["selectedName"];
           emojiName = _response["selectedEmoji"];
},

Second Screen

Navigator.pop(
       context,
       {"selectedName":_selectedName,"selectedEmoji": _selectedEmoji},
);

Upvotes: 0

MrPengueeen
MrPengueeen

Reputation: 59

I think you're returning the List of String in a wrong format. The strings need to be inside square brackets. Like this:

Navigator.pop(
       context,
       [_selectedName, _selectedEmoji],
);

And about the error "A non-null String must be provided to a Text widget.", this occurs when you're passing a null value to a Text Widget. You can change the Text Widgets in your second screen like this to check for null values and have a non-null dummy text inside the Text widgets and get rid of the error:

return Card(
                  child: ListTile(
                    leading: Text(targetElectricsEmojiLists[index] ?? 'NULL value here'),
                    title: Text(targetElectricsNameLists[index] ?? 'NULL value here'),
                    onTap: () {
                      setState(() {
                        _selectedName = targetElectricsNameLists[index];
                        _selectedEmoji = targetElectricsEmojiLists[index];
                      });
                      Navigator.pop(
                        context,
                        {_selectedName, _selectedEmoji},
                      );
                    },
                  ),
                );

Upvotes: 0

Related Questions