flutter - 参数类型'Future' 不能分配给参数类型 'bool'。当我尝试从 Firestore 中检索布尔值时
问题描述
在value: await statusonoff(device),
我收到一条错误消息,提示无法将参数类型“Future”分配给参数类型“bool”。当我从 Firestore 中检索一个布尔值时
static Future<bool>statusonoff(Device device) async {
DocumentSnapshot variable = await Firestore.instance
.collection('device-configs')
.document(device.id)
.get();
return variable.data['value']['on'];
}
CustomSwitch(
activeColor: Colors.green,
value: await statusonoff(device),
onChanged: (value) async {
DocumentSnapshot variable = await Firestore.instance
.collection('device-configs')
.document(device.id)
.get();
print(variable.data['value']['on']);
setState(() {
if (variable.data['value']['on'] == false) {
value = true;
_configMode = "on";
_updateDeviceConfig(context, device);
} else if (variable.data['value']['on'] == true) {
value = false;
_configMode = "off";
_updateDeviceConfig(context, device);
}
status = value;
});
},
),
解决方案
如果要使用boolean
从 Future 中检索到的,则需要使用 FutureBuilder。
return FutureBuilder<bool>(
//! remove the parameters from statusonoff (Device device)
// or you could make it future:statusonoff(device)
//istantiate this instance of Device before you call it
future:statusonoff,
builder: (context, variable){
if(variable.id.isNotEmpty)
return CustomSwitch(
//*put the rest of your code here
);
}
);
推荐阅读
- javascript - 如何在循环内使用回调?
- c# - 如何在平移手势识别器中访问我的总 x 翻译并从 XAML 访问它
- selenium-webdriver - 由于解析 Cucumber 选项时出错,无法在 Cucumber Framework 中创建测试 Runner 类
- ruby-on-rails - 在选择后使用 where 搜索子句
- excel - 如何阅读声明数组大小?
- pandas - 过滤每个 ID 的特定变量中具有相同值的行 - Pandas
- java - 这段代码有什么问题?(多数据源 Oracle)
- java - Java中的显示列表
- web-crawler - 什么是“Bytespider”用户代理?
- curl - 相当于 cURL 的请求返回错误