Home > Software engineering >  Why using <Null> after showDialog?
Why using <Null> after showDialog?

Time:11-16

Here I'm rendering an AlertDialog inside showDialog to display when error thrown from provider file. But that didn't work first time( Seems like stuck in catchError block, didn't execute future of catchError), Then I told to add <Null> after showDialog. That worked But How, What is changed, what it means?

Here is the code

if (_editedProduct.id == null) {
    Provider.of<Products>(context, listen: false)
        .addProduct(_editedProduct)
        .catchError((error) {
      return showDialog<Null>(     //Here is that Null I didn't understand
          context: context,
          builder: (ctx) {
            return AlertDialog(
              title: Text('ERROR'),
              content: Text('Error Occured'),
              actions: [
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text('I GOT IT'),
                )
              ],
            );
          });
    }).then((_) {
      print('THEN');
      setState(() {
        isLoading = false;
      });
      Navigator.of(context).pop();
    });
  } else {
    Provider.of<Products>(context, listen: false)
        .updateProduct(_editedProduct.id, _editedProduct);
    Navigator.of(context).pop();
  }
}

Nb: isLoading true shows a circluarProgressIndicator()

CodePudding user response:

From the Official Docs

Returns a Future that resolves to the value (if any) that was passed to Navigator.pop when the dialog was closed.

The place where you sent Null indicates the type of data the Future will return.

By using Null you are indicating that the Future won't return any data, this tell your program not to wait for the possible value that might be returned.

Suppose in your dialog user has to pick 2 numbers and you want the picked number to be returned to the place where you called the showDialog() function then you'll use int instead of Null.

Something like

showDialog<int>(...)
  • Related