我正在尝试实现一个开关,该开关使用以下密钥在我的Firestore中读取和写入bool值 设备/设备1 / LED_STATUS /数据
当我运行getStatus时,该值显示为null,然后给出该值。在该函数能够给出值之前,将先构建窗口小部件,因此会给出null断言异常并崩溃。我尝试了许多尝试将initState设置为setState的方法,但是没有用。
echo '{ "auths": { "https://awesome_docker_repsitory.com": { "auth": "c4ajLWdpqGxhYi3xYsQ6qVa4aUefY0xbd1t6dtc4YnQ3y2DYTDoOWONx", "email": "bitbucket@foo.bar" } } }' > ~/.docker/config.json"
建议后更新
我尝试了TEST按钮正常工作的建议方式,即它们正在更新Firebase数据库,但是我无法实现Switch!请帮忙!
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
class SwitchPage extends StatefulWidget {
static const String id = 'switch_screen';
@override
_SwitchPageState createState() => _SwitchPageState();
}
class _SwitchPageState extends State<SwitchPage> {
final DBref = FirebaseDatabase.instance.reference();
bool isSwitched;
void LedOn() async {
await DBref.child("DHT11")
.child("Device1")
.child("LED_STATUS")
.update({'DATA': 'TRUE'});
}
void LedOFF() async {
await DBref.child("DHT11")
.child("Device1")
.child("LED_STATUS")
.update({'DATA': 'FALSE'});
}
Future<bool> getStatus() async {
bool isSwitched = (await FirebaseDatabase.instance
.reference()
.child("DHT11/Device1/LED_STATUS/DATA")
.once())
.value;
print(isSwitched);
return isSwitched;
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Device Center'),
),
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
FlatButton(
onPressed: () {
getStatus();
print(isSwitched);
},
child: Text('TEST')),
Switch(
value: isSwitched,
onChanged: (value) {
if (isSwitched == 'TRUE') {
return LedOFF();
} else {
LedOn();
}
},
)
],
)),
);
}
}
答案 0 :(得分:0)
您应该执行以下操作:
Widget _createSwitch(){
if(isSwitched==null)
return Center();
return Switch(
value: isSwitched,
onChanged: (value) {
if (isSwitched == 'TRUE') {
return LedOFF();
} else {
LedOn();
}
},
);
}
并将getStatus更改为此:
void getStatus() async {
bool mswitch = (await FirebaseDatabase.instance
.reference()
.child("DHT11/Device1/LED_STATUS/DATA")
.once())
.value;
print(mswitch);
setState(() {
isSwitched=mswitch;
});
}
答案 1 :(得分:0)
您应该在getStatus方法中执行类似的操作
void getStatus() async {
String newValue = (await FirebaseDatabase.instance
.reference()
.child("DHT11/Device1/LED_STATUS/DATA")
.once())
.value;
print(isSwitched);
setState(() {
if( newValue == 'TRUE' ) {
isSwitched = true;
}
else {
isSwitched = false;
}
});
}
然后在FlatButton onPressed中执行类似的操作
onPressed: () async {
await getStatus();
print(isSwitched);
},
更新的代码 基于新要求
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
class SwitchPage extends StatefulWidget {
static const String id = 'switch_screen';
@override
_SwitchPageState createState() => _SwitchPageState();
}
class _SwitchPageState extends State<SwitchPage> {
final DBref = FirebaseDatabase.instance.reference();
bool isSwitched;
bool newVal;
void LedOn() async {
await DBref.child("DHT11")
.child("Device1")
.child("LED_STATUS")
.update({'DATA': 'TRUE'});
}
void LedOFF() async {
await DBref.child("DHT11")
.child("Device1")
.child("LED_STATUS")
.update({'DATA': 'FALSE'});
}
void getStatus() async {
String newValue = (await FirebaseDatabase.instance
.reference()
.child("DHT11/Device1/LED_STATUS/DATA")
.once())
.value;
print(isSwitched);
print(newValue);
setState(() {
if (newValue == 'TRUE') {
isSwitched = true;
} else {
isSwitched = false;
}
});
}
void _handleSwitch(bool value) async {
if( value ) {
await LedOn();
} else {
await LedOFF();
}
setState(() {
isSwitched = value;
});
}
@override
void initState() {
getStatus();
super.initState();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Device Center'),
),
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
FlatButton(
onPressed: () async {
await getStatus();
},
child: Text('TEST STATUS')),
FlatButton(
onPressed: () {
LedOn();
print('ON');
},
child: Text('LED ON')),
FlatButton(
onPressed: () {
LedOFF();
print('OFF');
},
child: Text('LED OFF')),
Switch(
value: isSwitched,
onChanged: (value) async {
await _handleSwitch(value);
},
),
],
)),
);
}
}