How to return bool value with navigator.pop?

I'm trying to return a bool value from the dialogbox but I do not understand why the value does not return as need. I have tried returning as a future value and returning the values together with context after popping the dialogbox.

final bool delete = await _showDialog();
print(delete);

Future<bool> _showDialog() {
bool result;

showDialog(
  context: context,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text('Delete Appointment'),
      content: Text(
        'Are you sure? \nThis action cannot undo.',
        style: TextStyle(
          color: Colors.red,
          fontSize: 20
        ),
      ),
      actions: <Widget>[
        FlatButton(
          color: Colors.blue,
          child: Text(
            'CANCEL',
            style: TextStyle(
              color: Colors.white,
              fontSize: 20
            ),
          ),
          onPressed: () {
            setState(() => result = false);
            //print(result);
            Navigator.pop(context, result);
            return Future.value(result);
          },
        ),
        SizedBox(
          width: 50,
        ),
        FlatButton(
          color: Colors.red,
          child: Text(
            'CONFIRM',
            style: TextStyle(
              color: Colors.white,
              fontSize: 20
            ),
          ),
          onPressed: () {
            setState(() => result = true);
            //print(result);
            Navigator.pop(context, result);
            return Future.value(result);
          },
        )
      ],
    );
  }
);
}

You pass the value you want to return to Navigator.pop and it will be returned by showDialog:

final result = await showDialog(
  ...
  // At some point, Navigator.pop is called
  Navigator.pop(context, true);
);

print(result); // Prints: true

Navigator.pop(context); has no return value because it is void , Hi, I have a question about this: I believe Navigator.pop(context) function has no return value because return type is void: bool� Python also has many built-in functions that returns a boolean value, like the isinstance() function, which can be used to determine if an object is of a certain data type: Example Check if an object is an integer or not:


Here's a working example based on your code:

@override
Widget build(BuildContext context) {
  return Container(
    child: RaisedButton(onPressed: () async {
      bool delete = await _showDialog(context);
      print(delete);
    })
  );
}

Future<bool> _showDialog(context) {
  return showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Delete Appointment'),
        content: Text(
          'Are you sure? \nThis action cannot undo.',
          style: TextStyle(
            color: Colors.red,
            fontSize: 20
          ),
        ),
        actions: <Widget>[
          FlatButton(
            color: Colors.blue,
            child: Text(
              'CANCEL',
              style: TextStyle(
                color: Colors.white,
                fontSize: 20
              ),
            ),
            onPressed: () {
              Navigator.pop(context, false);
            },
          ),
          SizedBox(
            width: 50,
          ),
          FlatButton(
            color: Colors.red,
            child: Text(
              'CONFIRM',
              style: TextStyle(
                color: Colors.white,
                fontSize: 20
              ),
            ),
            onPressed: () {
              Navigator.pop(context, true);
            },
          )
        ],
      );
    }
  );
}

Return data from a screen, If that method returns false, then the route remains in the Navigator's history (the to return the value selected by the user to the widget that created their route. A Boolean value, which is sometimes called a logical value, is one of several types of data used in spreadsheets in applications like Excel and Google Sheets.Named after the 19th-century mathematician George Boole, Boolean values are part of a branch of algebra known as Boolean algebra or Boolean logic.


check out this full woking code. you have to pass value which you want to return.

import 'package:flutter/material.dart';

class Delete extends StatefulWidget {
  Delete({Key key}) : super(key: key);

  @override
  _DeleteState createState() => _DeleteState();
}

class _DeleteState extends State<Delete> with SingleTickerProviderStateMixin {
  int index = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () {
            _showDialog().then((value) {
              print(value.toString());
            });
          },
          child: Text("press ME!"),
        ),
      ),
    );
  }

  _showDialog() {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Delete Appointment'),
            content: Text(
              'Are you sure? \nThis action cannot undo.',
              style: TextStyle(color: Colors.red, fontSize: 20),
            ),
            actions: <Widget>[
              FlatButton(
                color: Colors.blue,
                child: Text(
                  'CANCEL',
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
                onPressed: () {
                  Navigator.pop(context, false);
                },
              ),
              SizedBox(
                width: 50,
              ),
              FlatButton(
                color: Colors.red,
                child: Text(
                  'CONFIRM',
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
                onPressed: () {
                  Navigator.pop(context, true);
                },
              )
            ],
          );
        });
  }
}

pop method - Navigator class - widgets library, bool value = await Navigator.push(context, MaterialPageRoute<bool>( is used to return a value, the route's type parameter must match the type of pop's result. Consequently, when std::cout prints a value for b, it prints 0. Boolean return values. Boolean values are often used as the return values for functions that check whether something is true or not. Such functions are typically named starting with the word is (e.g. isEqual) or has (e.g. hasCommonDivisor).


onPressed:() {
  result = false;
  //print(result);
  setState(() {});

  Navigator.pop(context, result);
}

or

onPressed:() {
  setState(() {
    result = false;
    //print(result);
    Navigator.pop(context, result);
  });
}

Navigator class - widgets library - Dart API, Returns a Future that resolves to the value (if any) that was passed to Navigator. pop true).pop(result) to close the dialog rather just 'Navigator.pop(context, result)`. Future<T> customShowDialog<T>({ @required BuildContext context, bool� However, it is more common to return boolean values from boolean expressions, for conditional testing (see below). Boolean Expression A Boolean expression is a Java expression that returns a Boolean value: true or false .


For Return True

 Navigator.of(context).pop(true);

For Return False

 Navigator.of(context).pop(false);

customShowDialog function - CustomAlertDialog library, Future<bool> _onBackPressed() { if (!widget.editing) { Navigator.pop(context, you will just need to return a boolean value whenever your method completes its � So, passing a parameter is optional. It can return one of the two values. It returns True if the parameter or value passed is True. It returns False if the parameter or value passed is False. Here are few cases, in which Python’s bool() method returns false. Except these all other values return True. If a False value is passed. If None is passed.


How to return Future or any other types from a function in dart, And in the second screen modify the pop() function. Navigator.pop(context, true);. This will return the boolean value of true back to the first� Pop the top-most route off the navigator that most tightly encloses the given context. The current route's Route.didPop method is called first. If that method returns false, then the route remains in the Navigator's history (the route is expected to have popped some internal state; see e.g. LocalHistoryRoute).


Navigating between screens in Flutter: Navigator, named routes , When a route is used to return a value, the route's type parameter. /// must match the type of [pop]'s result. That's why we've used. /// `MaterialPageRoute<bool>`� If another developer reads your code and sees a return type of bool, he immediately understands that it returns a truth value. If instead he sees that it returns an int , he has to dig a little further and spend more time figuring out the interface to the function (e.g. reading documentation or reading the code), which slows him down.


packages/flutter/lib/src/widgets/navigator.dart - external , Returns false if this route wants to veto a [Navigator.pop]. This method is bool value = await Navigator.push(context, MaterialPageRoute<bool>(. /// builder:� An old thread, sure, but a popular one apparently. It's 2020 now and none of these answers have addressed the issue of unreadable code. @pimvdb's answer takes up less lines, but it's also pretty complicated to follow.