Retrieving, comparing and sending data from realtime database firebase to flutter

Viewed 106

what I'm trying to do is fetch data from realtime database in table picture_has_changed, and when pressed that it changes from 0 to 1 and 1 to 0.

enter image description here

This is what I have done so far, but when I try to convert the value it says that it isn't a integer value. So any help would be great.

The code:

  openGatePhoto() async {
    await Firebase.initializeApp();
  FirebaseStorage storage = FirebaseStorage.instance;
    var storageReference =
        storage.ref().child("picture_has_changed");

    if (storageReference == 0) {
      storageReference = "1";
    } else {
      storageReference = "0";
    }

I tried using set function also, but it didn't work here it is:

storageReference.child("picture_has_changed").set(val);

Thanks in advance :)

1 Answers

You're trying to access the Realtime Database through the Storage API. While both products are part of Firebase, their APIs are completely separate and you can't use the API for one to access the other.

To access the value from the Realtime Database, do something like this:

var reference =
    FirebaseDatabase.instance.ref().child("picture_has_changed");
var snapshot = await reference.once();

print(snapshot.value)

I'd also recommend checking out the usage instructions for this part of the library.

Related