【发布时间】:2020-06-09 17:59:48
【问题描述】:
我正在制作一个在 Firestore 中切换布尔值的测试应用。我需要应用程序从 Firestore 中检索开关的初始值。通过我的方法,我得到错误类型“Future”不是“bool”类型的子类型。布尔值称为 LEDOn。这是我的应用程序:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
final db = Firestore.instance;
void updateLED(bool newValue) async {
await db
.collection('LEDStatus')
.document('LEDStatus')
.updateData({'LEDOn': newValue});
}
Future<dynamic> checkLEDStatus() async {
DocumentSnapshot snapshot =
await db.collection('LEDStatus').document('LEDStatus').get();
return snapshot.data['LEDOn'];
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(brightness: Brightness.dark),
home: Scaffold(
appBar: AppBar(
title: Text('Thingspeak'),
),
body: Column(
children: <Widget>[
Switch(
value: checkLEDStatus(),
onChanged: (bool newValue) {
setState(() {
print(newValue);
updateLED(newValue);
});
})
],
),
),
);
}
}
【问题讨论】:
标签: firebase flutter google-cloud-firestore