【发布时间】:2021-06-06 21:24:40
【问题描述】:
我想知道如何在 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];
},
),
第二屏
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},
);
},
),
);
我认为这个问题在下面的部分。你知道写代码的其他方法吗?
第一个屏幕
onPressed: () async {
final List<String> _response =
await Navigator.pushNamed(
context, '/goToSecondScreen');
emoji = _response[0];
emojiName = _response[1];
},
第二屏
Navigator.pop(
context,
{_selectedName, _selectedEmoji},
);
我更改了代码。此外,我得到了错误。
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:类型“MaterialPageRoute”不是类型“Route?”的子类型在类型转换中
第一个屏幕
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];
},
),
第二屏
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],
);
},
),
);
【问题讨论】:
-
我尝试了以下答案。但是,我刚刚收到错误消息,[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'MaterialPageRoute
' is not a subtype of type 'Route - >? '在类型转换中