Flutter:关闭警报对话框后未返回future

问题描述 投票:-1回答:3

我在Flutter中创建了一个http方法,如下所示:

  Future<void> addProduct(Product product) {
    const url = 'https://flutter-shop-3de16.firebaseio.com/products';
    return http
        .post(
      url,
      body: json.encode({
        'title': product.title,
        'description': product.description,
        'imageUrl': product.imageUrl,
        'price': product.price,
        'isFavorite': product.isFavorite,
      }),
    )
        .then((response) {
      ...
      notifyListeners();
    }).catchError((error) {
      print(error);
      throw error;
    });
  }

[我知道我的url是错误的,因为我想得到错误。

。catchError中,我抛出一个错误,并且在提供商的主页上,我像这样使用addProduct

      Provider.of<Products>(context, listen: false)
          .addProduct(_editedProduct)
          .catchError((error) {
        return showDialog(
          context: context,
          builder: (ctx) => AlertDialog(
            title: Text('An error occurred!'),
            content: Text('Something went wrong.'),
            actions: <Widget>[
              FlatButton(
                child: Text('Okay'),
                onPressed: () {
                  Navigator.of(ctx).pop();
                },
              )
            ],
          ),
        );
      }).then((_) {
        print('after catch accoured');
        setState(() {
          _isLoading = false;
        });
        Navigator.of(context).pop();
      });
    }
  }

catchError我出错了,我显示了一个警告对话框。点击Okay按钮后,我想执行then块,所以我返回了showDialog,因为它返回了将来。

但是我不知道为什么

.then((_) {
            print('after catch accoured');
            setState(() {
              _isLoading = false;
            });
            Navigator.of(context).pop();
          });
        }

关闭警报对话框后不运行?

flutter android-alertdialog future flutter-dependencies
3个回答
0
投票

then块将在主Future之后运行;它不会在catchError之后运行。如果要在catchError之后运行一段代码,请使用whenComplete块。

  Provider.of<Products>(context, listen: false)
          .addProduct(_editedProduct)
          .catchError((error) {
        return showDialog(
          context: context,
          builder: (ctx) => AlertDialog(
            title: Text('An error occurred!'),
            content: Text('Something went wrong.'),
            actions: <Widget>[
              FlatButton(
                child: Text('Okay'),
                onPressed: () {
                  Navigator.of(ctx).pop();
                },
              )
            ],
          ),
        );
      }).whenComplete(() { // This will run after execution
        print('after catch accoured');
        setState(() {
          _isLoading = false;
        });
        Navigator.of(context).pop();
      });
    }
  }

0
投票

下面的代码应该可以解决您的问题。在显示对话框之后移动then语句将使其再次工作。

    Provider.of<Products>(context, listen: false)
        .addProduct(_editedProduct)
        .catchError((error) {
      return showDialog(
        context: context,
        builder: (ctx) =>
            AlertDialog(
              title: Text('An error occurred!'),
              content: Text('Something went wrong.'),
              actions: <Widget>[
                FlatButton(
                  child: Text('Okay'),
                  onPressed: () {
                    Navigator.of(ctx).pop();
                  },
                )
              ],
            ),
      ).then((_) {
        print('after catch accoured');
        setState(() {
          isLoading = false;
        });
        Navigator.of(context).pop();
      });;
    });

-1
投票
return showDialog(
         context: context,
         builder: (ctx) => AlertDialog(
           title: Text('An error occurred!'),
           content: Text('Something went wrong.'),
           actions: <Widget>[
             FlatButton(
               child: Text('Okay'),
               onPressed: () {
                 Navigator.of(ctx).pop();
               },
             )
           ],
         ),
       ).then((value) => null);

以上then((_) => null );将解决您的问题,我认为会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.