我想知道如何在 Flutter 上使用 pop 获取两个值。我尝试编写代码。但我得到了异常“必须向文本小部件提供非空字符串。”
这是代码。
第一屏
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];
},
),
Run Code Online (Sandbox Code Playgroud)
第二屏
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},
);
},
),
);
Run Code Online (Sandbox Code Playgroud)
我认为这个问题在下面部分。您知道编写代码的其他方法吗?
第一屏
onPressed: () async {
final List<String> _response =
await Navigator.pushNamed(
context, '/goToSecondScreen');
emoji = _response[0];
emojiName = _response[1];
},
Run Code Online (Sandbox Code Playgroud)
第二屏
Navigator.pop(
context,
{_selectedName, _selectedEmoji},
);
Run Code Online (Sandbox Code Playgroud)
[错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:类型“MaterialPageRoute”不是类型“Route<List>”的子类型?在类型转换中
第一屏
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];
},
),
Run Code Online (Sandbox Code Playgroud)
第二屏
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],
);
},
),
);
Run Code Online (Sandbox Code Playgroud)
你可以尝试下面的方法
第一屏
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'];
});
},
),
Run Code Online (Sandbox Code Playgroud)
第二屏
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}
);
},
),
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7758 次 |
最近记录: |