【发布时间】:2020-06-16 03:44:38
【问题描述】:
您好,当我单击按钮从 true 到 false 并从 false 到 true 时,我试图更改 firebase 中的数据,但是当我单击数据库中的值时,它变为 null。 有谁知道出了什么问题?谢谢。
class _MyHomePageState extends State<MyHomePage> {
bool selected;
@override
void initState() {
super.initState();
setInitialValue();
}
setInitialValue() async {
final value = await _incrementCounter();
setState(() {
selected = value?? true;
});
}
_loadCounter(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('select', value);
}
Future<bool> _incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool boolValue = prefs.getBool('select');
return boolValue;
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(child: Text('ola'))),
MaterialButton(
color: Theme.of(context).primaryColor,
child: Text( 'Add', style: TextStyle(color: Colors.white), ),
onPressed: () {
Firestore.instance.collection('teste').document('eHJzjbq6c7MwrObrR9XY').updateData({'mover' : selected});
}
),
],
),
),
);
}
}
【问题讨论】:
标签: firebase flutter google-cloud-firestore