未处理的异常:类型 'Null' 不是类型 'Future<Never>' 的子类型
Unhandled Exception: type 'Null' is not a subtype of type 'Future<Never>'
我已经尝试将 Future 和字符串都设为 nullable,但出了点问题请帮忙?
floatingActionButton: FloatingActionButton(
onPressed: () {
getName().then((value) {
print(value);
throw("error!!!!!");
}).catchError((error) => print(error.toString()));
},
child: Icon(Icons.add),
),
在我的 class
某处的构建方法之外
Future<String> getName() async =>'Some String';
你可以试试这个
getName().then((value) {
print(value);
throw ("error!!!!");
}).catchError((error) {
print("catch error :: " + error.toString());
});
该代码打印 catch error :: error!!!!
。是你想要的吗?
所以这修复了我的代码
floatingActionButton: FloatingActionButton(
onPressed: () => getName().then((value){
// the return type of an error handler is not acceptable
// as the value of future.
// after lambda expression is executed it gone so
// we wouldn't know where the error came from
// because also the return type is dynamic so we
//can't get to a datatype interface so it may be null since it's dynamic
if(value is FutureOr) {
throw("error");
}
}).catchError((error) => print(error.toString()) ) ,
我已经尝试将 Future 和字符串都设为 nullable,但出了点问题请帮忙?
floatingActionButton: FloatingActionButton(
onPressed: () {
getName().then((value) {
print(value);
throw("error!!!!!");
}).catchError((error) => print(error.toString()));
},
child: Icon(Icons.add),
),
在我的 class
某处的构建方法之外Future<String> getName() async =>'Some String';
你可以试试这个
getName().then((value) {
print(value);
throw ("error!!!!");
}).catchError((error) {
print("catch error :: " + error.toString());
});
该代码打印 catch error :: error!!!!
。是你想要的吗?
所以这修复了我的代码
floatingActionButton: FloatingActionButton(
onPressed: () => getName().then((value){
// the return type of an error handler is not acceptable
// as the value of future.
// after lambda expression is executed it gone so
// we wouldn't know where the error came from
// because also the return type is dynamic so we
//can't get to a datatype interface so it may be null since it's dynamic
if(value is FutureOr) {
throw("error");
}
}).catchError((error) => print(error.toString()) ) ,