skip to Main Content

I am using firebase realtime database with my Flutter Mobile Application. I am saving realtime data from an IoT device, a cloud function analyzes the data and generates analytics for my data on daily basis. As in following Database Structure, where "UsersData" is the root node, followed by user-uid and then his realtime readings and analytics.

enter image description here

Analytics are nested/saved as in following tree.
Analytics contains data properly laid yearly as follow.

enter image description here

I want to fetch data for only single day, when I run the following query on Android it works totally fine and it return me data for single day. But on iOS it returns data for entire node.


  /// Getting Analytics for today Data
  Future getAnalyticsforSelectedDate({
    required String uid,
    required DateTime selectedDate,
  }) async {
    String selectedYear = selectedDate.year.toString();
    String selectedMonth = selectedDate.month.toString();
    String selectedDay = selectedDate.day.toString();

// Format Add a zero in front of single digit date such as
// e.g. Turn 9/9/2023 into 09/09/2023

    if (selectedDate.day >= 10) {
      selectedDay = selectedDate.day.toString();
    } else {
      selectedDay = "0" + selectedDate.day.toString();
    }

    if (selectedDate.month >= 10) {
      selectedMonth = selectedDate.month.toString();
    } else {
      selectedMonth = "0" + selectedDate.month.toString();
    }

    try {
      final data = await _db
          .child(
              "UsersData/$uid/analytics/$selectedYear/$selectedMonth/$selectedDay")
          .get();

      log("Date = $selectedDay/$selectedMonth/$selectedYear");
      log("${Platform.isIOS ? "IOS Results" : "Android Result"}:: ${data.value} ");

      return data.value;
    } catch (e) {
      log("@getAnalyticsforSelectedDate " + e.toString());
    }
  }

I am using latest firebase packages with Flutter Version 3.7.10.

firebase_auth: ^4.9.0
firebase_core: ^2.15.1
firebase_core_platform_interface: ^4.8.0
firebase_database: ^10.2.5
firebase_storage: ^11.2.6

I have tried running on iOS 16 and iOS 12 to see if it was a compatibility issue with old or new iOS version(same results on both).

Here what happens when I run the code for Date 18/09/2023 on Android
Result logs on Android

Here is the log for iOS on Date 18/09/2023
iOS logs

2

Answers


  1. You’re using the get() method, and I recall seeing some data loading issues with that on iOS at some point.

    If you are affected by that issue, you should be able to use once() to work around it. If that indeed addresses it, I’ll raise an issue with the engineering team for the bug you hit.

    Login or Signup to reply.
  2. i also have the same problem. And even on https://www.youtube.com/watch?v=sXBJZD0fBa4&t=2352s&ab_channel=Firebase the guy says that we shall not use once if we really want to get real time data from database since once is not always returning real data since the local storage was introduced

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search