admin管理员组文章数量:1435859
I would like some help using a cancel
button to pop the current modal popup, but not return the required data.
Here is the code that requires an EntryData
class on return from the modal popup DataEntryView
:
Future<void> _addEvent(BuildContext context) async {
// this Navigator.push returns a Future that completes after calling
// Navigator.pop on the DataEntryView.
EntryData result = await Navigator.of(context).push(
CupertinoPageRoute(
fullscreenDialog: true,
builder: (context) {
return const DataEntryView();
},
),
);
Here is the code for the cancel button in the DataEntryView
:
// Cancel button
const SizedBox(height: 30),
CupertinoButton(
onPressed: () {
Navigator.pop(context, newEntry);
},
child: const Text('Cancel'),
),
In the cancel
button I am sending back an EntryData
named newEntry
with dummy info to test to see if it's real:
EntryData newEntry = EntryData(2000, "Blank", "Do Not Use");
But is there another way to tell the Future
that there is no data and just pop the modal view? Because now I have to write code to test newEntry
to see if it's a dummy.
Any help would be appreciated. I'm new to Flutter.
I would like some help using a cancel
button to pop the current modal popup, but not return the required data.
Here is the code that requires an EntryData
class on return from the modal popup DataEntryView
:
Future<void> _addEvent(BuildContext context) async {
// this Navigator.push returns a Future that completes after calling
// Navigator.pop on the DataEntryView.
EntryData result = await Navigator.of(context).push(
CupertinoPageRoute(
fullscreenDialog: true,
builder: (context) {
return const DataEntryView();
},
),
);
Here is the code for the cancel button in the DataEntryView
:
// Cancel button
const SizedBox(height: 30),
CupertinoButton(
onPressed: () {
Navigator.pop(context, newEntry);
},
child: const Text('Cancel'),
),
In the cancel
button I am sending back an EntryData
named newEntry
with dummy info to test to see if it's real:
EntryData newEntry = EntryData(2000, "Blank", "Do Not Use");
But is there another way to tell the Future
that there is no data and just pop the modal view? Because now I have to write code to test newEntry
to see if it's a dummy.
Any help would be appreciated. I'm new to Flutter.
Share Improve this question asked Nov 15, 2024 at 21:28 TM LynchTM Lynch 5043 silver badges14 bronze badges 1- 1 Common convention here is to use a nullable, so it will be expecting either a newEntry or null on cancel or error. – Randal Schwartz Commented Nov 15, 2024 at 21:49
1 Answer
Reset to default 1Try changing the result
variable from a EntryData
to an EntryData?
which means "either an EntryData
or null
".
EntryData? result = await Navigator.of(context).push(
CupertinoPageRoute(
fullscreenDialog: true,
builder: (context) {
return const DataEntryView();
},
),
);
Then if you run Navigator.pop(context)
with no second parameter, result
will be null, and you can do something different in that case:
if (result == null) {
// do something
}
New cancel button code:
// Cancel button
const SizedBox(height: 30),
CupertinoButton(
onPressed: () {
Navigator.pop(context); // pop with no result
},
child: const Text('Cancel'),
),
本文标签: navigationHow do I pop a view with Cancel in Flutter if a return is expectedStack Overflow
版权声明:本文标题:navigation - How do I pop a view with Cancel in Flutter if a return is expected? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745675804a2669843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论