Reading data from Firebase RTDB using Flutter has different behavior on Android and iOS

Viewed 121

I'm experiencing a behavior where the following call behaves differently when executed for iOS and for Android.

In Android, the following .get() call returns the expected snapshot from the chatRoomID path. However, in iOS, .get() ends up returning a snapshot of the whole node under myUser.userID.

It seems for iOS, the second child node path is disregarded...

DataSnapshot snapshot = await chatsRef
                          .child(myUser.userID!)
                          .child(chatRoomID)
                          .get();
print(snapshot.value);

JSON:

{
"chats" : {
  "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : {
    "c00dca80-9077-11ec-855a-910961fc4253" : {
      "chatAdmin" : "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1",
      "chatImage" : "https://images.pexels.com/photos/887827/pexels-photo-887827.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
      "chatName" : "chef",
      "chatRoomID" : "c00dca80-9077-11ec-855a-910961fc4253",
      "isActivityChat" : false,
      "isGroupChat" : true,
      "lastMessage" : {
        "lastMessage" : "Hey",
        "lastMessageTime" : "2022-02-18 00:00:56.992308",
        "messageID" : "-MwAC5BUz3GKPmkA1SSQ",
        "nKDsrLrcU0PgtEDV5tKpMumSDuu1" : "true",
        "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : "false",
        "psJQRp96VGWIjTDpNpMUShPNWa82" : "true",
        "sendBy" : "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1",
        "senderName" : "Emily"
      },
      "muted" : {
        "nKDsrLrcU0PgtEDV5tKpMumSDuu1" : false,
        "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : false,
        "psJQRp96VGWIjTDpNpMUShPNWa82" : false
      },
      "users" : {
        "nKDsrLrcU0PgtEDV5tKpMumSDuu1" : true,
        "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : true,
        "psJQRp96VGWIjTDpNpMUShPNWa82" : true
      }
    },
    "nKDsrLrcU0PgtEDV5tKpMumSDuu1_oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : {
      "chatRoomID" : "nKDsrLrcU0PgtEDV5tKpMumSDuu1_oF1b6J4Hz3NGzRb9RmSVFGJdcYi1",
      "isActivityChat" : false,
      "isGroupChat" : false,
      "lastMessage" : {
        "lastMessage" : "Shut",
        "lastMessageTime" : "2022-02-18 00:01:30.161511",
        "messageID" : "-MwACDHlTWpQhhQFh9k8",
        "nKDsrLrcU0PgtEDV5tKpMumSDuu1" : "true",
        "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : "false",
        "sendBy" : "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1",
        "senderName" : "Emily"
      },
      "muted" : {
        "nKDsrLrcU0PgtEDV5tKpMumSDuu1" : false,
        "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : false
      },
      "users" : {
        "nKDsrLrcU0PgtEDV5tKpMumSDuu1" : true,
        "oF1b6J4Hz3NGzRb9RmSVFGJdcYi1" : true
      }
    }
  }
},
}

enter image description here

1 Answers

As discussed in the comments

You should be getting the same behavior on both iOS and Android, as the underlying SDKs should work the same on those platforms. That said, since the FlutterFire libraries wrap the native SDKs and the get() API is relatively new, it might be that there is an unintended difference between how the iOS and Android SDKs implement it.

If that is indeed the case, you might want try and see if once() works for you. While it has an annoying side-effect in an edge-case (see here), the once API itself is much older, so more likely to be stable across platforms.


Update: I'm having a really hard time reproducing the problem. I've imported your JSON into a database of mine, and then run this code on startup of my app:

FirebaseDatabase database = FirebaseDatabase.instance;
var chatsRef = database.ref('71163140/chats');
var threadRef = chatsRef.child('oF1b6J4Hz3NGzRb9RmSVFGJdcYi1').child('nKDsrLrcU0PgtEDV5tKpMumSDuu1_oF1b6J4Hz3NGzRb9RmSVFGJdcYi1');
DataSnapshot snapshot = await threadRef.get();
if (snapshot.value != null) {
  print('get: ${snapshot.value}');
}

DatabaseEvent event = await threadRef.once();
if (event.snapshot.value != null) {
  print('once: ${event.snapshot.value}');
}

So it reads a single chat thread out of the data you shared with both get() and once(), and then prints what it gets. Both on iOS and Android, I never see the c00dca80-9077-11ec-855a-910961fc4253 thread showing up in my output, I only get the chat thread where "isGroupChat" : false,.

Can you update the code or data in your question to show how I can get the same faulty result as you get?

Related