错误:flutter/lib/ui/ui_dart_state.cc(157) 未处理的异常:类型 'Future<dynamic>' 不是类型 'FutureOr<Null>
Posted
技术标签:
【中文标题】错误:flutter/lib/ui/ui_dart_state.cc(157) 未处理的异常:类型 \'Future<dynamic>\' 不是类型 \'FutureOr<Null> 的子类型【英文标题】:ERROR:flutter/lib/ui/ui_dart_state.cc(157) Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'FutureOr<Null>错误:flutter/lib/ui/ui_dart_state.cc(157) 未处理的异常:类型 'Future<dynamic>' 不是类型 'FutureOr<Null> 的子类型 【发布时间】:2020-05-09 10:32:19 【问题描述】:我正在尝试在 showDialog 中处理来自 http 请求的错误,然后抛出一个错误,但我正面临这个错误
错误
E/flutter (18769): #13 TextInput._handleTextInputInvocation 包:flutter/.../services/text_input.dart:968 E/flutter (18769): #14 MethodChannel._handleAsMethodCall 包:flutter/…/services/platform_channel.dart:402 E/flutter (18769):#15 MethodChannel.setMethodCallHandler。包:flutter/…/services/platform_channel.dart:370 E/颤振 (18769):#16 _DefaultBinaryMessenger.handlePlatformMessage 包:flutter/…/services/binding.dart:200 E/flutter (18769): #17 _调用3。 (dart:ui/hooks.dart:303:15) E/flutter (18769): #18 _rootRun (dart:async/zone.dart:1126:13) E/flutter (18769): #19 _CustomZone.run (dart:async/zone.dart:1023:19) E/颤振(18769):#20 _CustomZone.runGuarded (dart:async/zone.dart:925:7) E/flutter (18769): #21 _invoke3 (dart:ui/hooks.dart:302:10) E/flutter (18769): #22 _dispatchPlatformMessage (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);
);
【问题讨论】:
【参考方案1】:因为你的函数类型是 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;
);
你的网址不正确,把'https://flutter-shop-768a7.firebaseio.com/products.jon'
改成'https://flutter-shop-768a7.firebaseio.com/products.json'
【讨论】:
【参考方案2】:请像这样在下面添加“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')),
],
),
);
【讨论】:
【参考方案3】:这是因为您没有指定.then((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
【讨论】:
以上是关于错误:flutter/lib/ui/ui_dart_state.cc(157) 未处理的异常:类型 'Future<dynamic>' 不是类型 'FutureOr<Null>的主要内容,如果未能解决你的问题,请参考以下文章
Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误