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
Add a comment  | 

1 Answer 1

Reset to default 1

Try 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