Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see changes in flutter app when changing values in Firebase Realtime Database?

enter image description here

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

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'});
           
like image 336
Ahmed A. Karasneh Avatar asked Mar 31 '20 01:03

Ahmed A. Karasneh


People also ask

How do you display data from Firebase in Flutter app?

To use Flutter with Firebase, you will first need to set dependencies in the pubspec file. You will also have to import firestore , i.e., the database provided by Firebase for data handling. Now, import the Firebase dependencies into your Dart file. import 'package:cloud_firestore/cloud_firestore.

How do you get real time data on Flutter?

To experience real-time database integration: Open the browser with Backendless Console and run the created Flutter app. Create a new movie record in Backendless Console's Data Browser, you will see the new movie cell will be rendered immediately in the Flutter app.


1 Answers

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');

      ...

    });
like image 136
Frank van Puffelen Avatar answered Nov 02 '22 22:11

Frank van Puffelen