繁体   English   中英

如何将切换开关按钮从颤动添加到 firebase firestore 数据库

[英]How to add the toggle switch button from flutter to firebase firestore database

我正在尝试设置一个从 flutter 到 firestore 的切换开关按钮。 我已经在我的 flutter 项目中设置了依赖项,但是,我不知道如何将 switch 与 firestore 连接。

我正在尝试制作一个可用于控制灯光的开/关开关; 我试过给它一些值,但即使如此,我也不知道如何与 firestore 连接。

class _HomeState extends State<Home> {

 bool _value = false;

  void _onChanged(bool value) {
   setState(() {
    _value = value; 
   });

  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home ${widget.user.email}'),
      ),
      body: new Container(
        padding: new EdgeInsets.all(32.0),
        child: new Column(
          children: <Widget>[
            new SwitchListTile.adaptive(
                title: new Text('Bedroom light'),
                activeColor: Colors.red,
                secondary: const Icon(Icons.lightbulb_outline),
                value: _value,
                onChanged: (bool value) {
                  _onChanged(value);

                })
          ],
        ),
      ),
    );
  }
}

这是我到目前为止的代码。 我知道我们必须使用 StreamBuilder,但我想知道如何使用。

您必须先在 firestore 中创建一个数据库引用,例如-

databaseReference = Firestore.instance.collection('Switches').where('switch','==',/*ANY NAME*/);

然后运行一个转换来更新 value 的值

Firestore.instance.runTransaction((transaction) async {
    await transaction.update(
    documentReference, _value);
};

只要确保在 firestore 中,将采用_value值的_valueboolean

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM