Flutter 无法将未来数据转换为精确数据
我正在使用Future从Firebase数据库返回一些返回Future的信息。现在我想转换bool。这里我正在刻面null。但是在then函数中我找到了我的值,但当我返回它时,它仍然为nullFlutter 无法将未来数据转换为精确数据,flutter,dart,Flutter,Dart,我正在使用Future从Firebase数据库返回一些返回Future的信息。现在我想转换bool。这里我正在刻面null。但是在then函数中我找到了我的值,但当我返回它时,它仍然为null Future<bool> _isfav(String post_key) async { bool _is_fav; await FirebaseDatabase.instance .reference() .child(Common.user) .chil
Future<bool> _isfav(String post_key) async {
bool _is_fav;
await FirebaseDatabase.instance
.reference()
.child(Common.user)
.child(Common.number)
.child("favourite")
.child(post_key)
.once()
.then((v) {
print(v.value);
if (v.value != null) {
_is_fav = true;
} else {
_is_fav= false;
}
}).catchError((err) => print(err));
return _is_fav; }
当我将数据打印到then函数中时,它将显示我的数据,但当我返回它时,它将返回null
我想从这里调用函数
child: Icon(
Icons.favorite,
color:read_fav(_key[index])!=null && read_fav(_key[index]) == true
? Colors.blue
: Colors.black,
)),
只需按照如下代码操作
Future<bool> _isfav(String post_key) async {
bool _is_fav;
var snapData = await FirebaseDatabase.instance
.reference()
.child(Common.user)
.child(Common.number)
.child("favourite")
.child(post_key)
.once();
if (v.value != null) {
_is_fav = true;
} else {
_is_fav = false;
}
return _is_fav;
}
Future<bool> read_fav(String index) async{
bool data=false;
data=await _isfav(index);
return data;
}
只需参考dart WAIT,然后在
你们都准备好了。实际上我正在检查这里孩子:图标(Icons.favorite,颜色:read\u fav(\u key[index])!=null和read\u fav(\u key[index]))==真彩色。蓝色:彩色。黑色,),
Future<bool> _isfav(String post_key) async {
bool _is_fav;
var snapData = await FirebaseDatabase.instance
.reference()
.child(Common.user)
.child(Common.number)
.child("favourite")
.child(post_key)
.once();
if (v.value != null) {
_is_fav = true;
} else {
_is_fav = false;
}
return _is_fav;
}
Future<bool> read_fav(String index) async{
bool data=false;
data=await _isfav(index);
return data;
}
bool isReadFav=await read_fav(index);