navigation - How do I pop a view with Cancel in Flutter if a return is expected? - Stack Overflow
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'),
),
- Intel兵屯深圳,意欲何为?
- Power apps - Multiple Sharepoint lists combine as collection and display in one gallery - Stack Overflow
- Why is my openGL render failing, Python OpenGL - Stack Overflow
- rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow
- javascript - In Quill, how to add one button to the toolbar to add some text? - Stack Overflow
- javascript - Alphabetize options in a select list, retain event listeners - Stack Overflow
- wpf - Installation of an upgrade version starting at ResumeDlg - Stack Overflow
- python - aiohttp showing 403 error but requests.get giving 200 response - Stack Overflow
- typescript - How to correctly mock an express middleware with jest? - Stack Overflow
- spring boot - Java - Implement @Component class dynmically - Stack Overflow
- augmented reality - Applying 2d image to a 3d object in xcode tutorials? - Stack Overflow
- c++ - Incremental compilation using Makefile - Stack Overflow
- javascript - How to properly handle AES encryption in React Native and generate Random Key for AES encryption? - Stack Overflow
- python - mecab-python3 AWS lambda "circular import" error - Stack Overflow
- Has Paho MQTT Client for Ionic on Android limitations regarding the number of client instances? - Stack Overflow
- visual studio code - VSCode support fully integrated IPython terminal - Stack Overflow
- javascript - How can I create hyperlinkOrPicture column types on SharePoint Lists with Microsoft Graph API or Sharepoint API? -