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],
);
},
),
);