在Firebase实时数据库中更改值时,如何查看flutter应用程序中的更改? [英] How to see changes in flutter app when changing values in Firebase Realtime Database?

查看:69
本文介绍了在Firebase实时数据库中更改值时,如何查看flutter应用程序中的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在Firebase实时数据库中进行任何更改时,我都试图使这些滚动开关更改其值.

I am trying to make these rolling switches to change its value whenever I do any change in Firebase realtime database.

更具体地说,每当我将Relay1/Data的值更改为0时,我都希望该开关变为非活动状态.

To be more specific, whenever I change the value of Relay1/Data to 0, I want that switch to become inactive.

我已经尝试过并且四处查看,但是找不到任何解决方法.

I've tried and looked everywhere, but I couldn't find any solution.

  bool relay1pressed;
  final databaseReferenceTest = FirebaseDatabase.instance.reference();



@override
  void initState() {
    super.initState();

    databaseReferenceTest
        .child('MedicalCenter')
        .once()
        .then((DataSnapshot snapshot) {
      String value = snapshot.value['Relay1']['Data'];
      print('Value is $value');
      if (value == '1') {
        relay1pressed = true;
      } else
        relay1pressed = false;

      setState(() {
        isLoading = true;
      });
    });
  }

  
//Widget build

            StreamBuilder(
              stream: databaseReferenceTest
                  .child('MedicalCenter')
                  .child('Relay1')
                  .onValue,
              builder: (BuildContext context, AsyncSnapshot<Event> snapshot) {
                databaseReferenceTest
                    .child('MedicalCenter')
                    .once()
                    .then((DataSnapshot snapshot) {
                  String value = snapshot.value['Relay1']['Data'];
                  print('Value is $value');
                  if (value == '1') {
                    relay1pressed = true;
                    print('relay1 bool $relay2pressed');
                  } else {
                    relay1pressed = false;
                    print('relay1 bool $relay2pressed');
                  }
                });

                return LiteRollingSwitch(
                  value: relay1pressed,
                  textOn: 'active',
                  textOff: 'inactive',
                  colorOn: Colors.deepOrange,
                  colorOff: Colors.blueGrey,
                  iconOn: Icons.lightbulb_outline,
                  iconOff: Icons.power_settings_new,
                  onChanged: (bool state) {
                    state
                        ? databaseReferenceTest
                            .child('MedicalCenter')
                            .update({'Relay1/Data': '1'})
                        : databaseReferenceTest
                            .child('MedicalCenter')
                            .update({'Relay1/Data': '0'});
           

推荐答案

您当前正在使用 once()从数据库中获取值,这意味着它仅读取当前值.如果要继续监视该值,则可以改用 onValue .

You're currently using once() to get the value from the database, which means it only reads the current value. If you want to keep monitoring the value, you'll want to use onValue instead.

databaseReferenceTest
    .child('MedicalCenter')
    .onValue.listen((event) {
      var snapshot = event.snapshot

      String value = snapshot.value['Relay1']['Data'];
      print('Value is $value');

      ...

    });

这篇关于在Firebase实时数据库中更改值时,如何查看flutter应用程序中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆