当前位置: 首页 > 知识库问答 >
问题:

错误:颤振/lib/ui/ui\u省道\u状态。cc(157)未处理的异常:类型“Future”不是类型“FutureOr的子类型

江光明
2023-03-14

我试图在showDialog中处理http请求的错误,然后抛出一个错误,但我遇到了这个错误

错误

E/颤振(18769):#13文本输入_HandletExtInput职业包:颤振/../services/text\u输入。飞镖:968 E/颤振(18769):#14通道_handleAsMethodCall包:颤振/../services/platform\u频道。dart:402E/颤振(18769):#15通道。setMethodCallHandler。包装:颤振/../services/平台\通道。飞镖:370E/颤振(18769):#16
\u默认双星信使。handlePlatformMessage包:颤振/../services/binding。飞镖:200 E/颤振(18769):17。(dart:ui/hooks.dart:303:15)E/flatter(18769):#18_rootRun(dart:async/zone.dart:1126:13)E/flatter(18769):#19_CustomZone。运行(dart:async/zone.dart:1023:19)E/flatter(18769):#20_自定义区域。横档(dart:async/zone.dart:925:7)E/flatter(18769):#21#u invoke3(dart:ui/hooks.dart:302:10)E/flatter(18769):#22
u调度平台消息(dart:ui/hooks.dart:162:5)


  Future<void> addProduct(Product product) {
    const url = 'https://flutter-shop-768a7.firebaseio.com/products.jon';
    return http
        .post(url,
            body: json.encode({
              'title': product.title,
              'description': product.description,
              'imageUrl': product.imageUrl,
              'price': product.price,
              'isFavorite': product.isFavorite
            }))
        .then((response) {
      final newProduct = Product(
          title: product.title,
          description: product.description,
          imageUrl: product.imageUrl,
          price: product.price,
          id: json.decode(response.body)['name']);
      // _items.insert(index, element)
      _items.add(newProduct);
      notifyListeners();
    }).catchError((error) {
      throw error;
    });
  }

     Provider.of<Products>(context, listen: false)
          .addProduct(_edditedProduct)
          .catchError((error) {
        return showDialog(
          context: context,
          builder: (ctx) => AlertDialog(
            title: Text('An Error occurred!'),
            content: Text('Someghing went wrong'),
            actions: <Widget>[
              FlatButton(
                  child: Text('ok'),
                  onPressed: () async => Navigator.of(context).pop())
            ],
          ),
        );
      }).then((_) {
        print('this is then function');
        setState(() {
          _isLoading = false;
        });
        Navigator.pop(context);
      });

共有3个答案

邢宏浚
2023-03-14

发生这种情况是因为您没有指定的返回类型。然后((response){})方法,来解决这个简单的更改

 .then((response) {
  final newProduct = Product(
      title: product.title,
      description: product.description,
      imageUrl: product.imageUrl,
      price: product.price,
      id: json.decode(response.body)['name']);
  // _items.insert(index, element)
  _items.add(newProduct);
  notifyListeners();
})

.then<void>((response) {
  final newProduct = Product(
      title: product.title,
      description: product.description,
      imageUrl: product.imageUrl,
      price: product.price,
      id: json.decode(response.body)['name']);
  // _items.insert(index, element)
  _items.add(newProduct);
  notifyListeners();
})de here
蒋茂
2023-03-14

请像这样在下面加上'Null'。添加问题解决后,我也面临同样的问题。

    return showDialog<Null>(
      context: context,
      builder: (ctx) => AlertDialog(
        title: Text('Error occurred!'),
        content: Text('Something went wrong...'),
        actions: [
          FlatButton(
              onPressed: () {
                Navigator.of(ctx).pop();
              },
              child: Text('Okay')),
        ],
      ),
    );
酆出野
2023-03-14

这是因为你的函数类型是Future,你的返回类型必须是Future,但是当你面对一个错误时,你的响应会抛出一个错误并返回Null,所以最好这样写你的异步函数

addProduct(Product product) async {
    const url = 'https://flutter-shop-768a7.firebaseio.com/products.json';
    await http
        .post(url,
            body: json.encode({
              'title': product.title,
              'description': product.description,
              'imageUrl': product.imageUrl,
              'price': product.price,
              'isFavorite': product.isFavorite
            }))
        .then((response) {
      final newProduct = Product(
          title: product.title,
          description: product.description,
          imageUrl: product.imageUrl,
          price: product.price,
          id: json.decode(response.body)['name']);
      // _items.insert(index, element)
      _items.add(newProduct);
      notifyListeners();
    }).catchError((error) {
      throw error;
    });
  }

您的url不正确更改'https://flutter-shop-768a7.firebaseio.com/products.jon“”https://flutter-shop-768a7.firebaseio.com/products.json“

 类似资料: