1

I'm having a problem with my Flutter SDK code where when I call the function to fetch data from the databaseReference it returns all the data from my database (8950 items), even though I'm doing the correct filter. But this problem only happens in the iOS debug on Android and it's returning only 1 exact item from the search.

DatabaseReference Function(String id) get productByIdRef =>
      (String id) => _database.ref('products/$id');
      
  StreamSubscription<DatabaseEvent>? _productsSubscription;

  Future<ProductData> getProductInScanner(String id) async {
    final cleanId = id.trim();
    final snapshot = await productByIdRef(cleanId).get();

    if (!snapshot.exists) {
      return ProductData(
        id: "",
        brands: "",
        composition: "",
        country: "",
        name: "",
        urlImage: "",
      );
    }

    final barcode = snapshot.key;
    final val = snapshot.value;

    try {
      Map<String, dynamic> map = {};
      if (val! is Map) {
        return ProductData(
          id: "",
          brands: "",
          composition: "",
          country: "",
          name: "",
          urlImage: "",
        );
      }
      if (val is Map) {
        map = {...Map<String, dynamic>.from(val)};
      }
      Map<String, dynamic> filtered = {};
      if (map.length > 1) {
        filtered = Map.fromEntries(
          map.entries
              .where((e) => e.key == id.trim())
              .map((e) => MapEntry(e.key, Map<String, dynamic>.from(e.value))),
        );
      }

      if (filtered.isEmpty) {
        return ProductData(
          id: "",
          brands: "",
          composition: "",
          country: "",
          name: "",
          urlImage: "",
        );
      }

      return ProductData.fromMap(map);
    } catch (_) {
      throw FormatException('Erro ao fomatar dados do produto');
    }
  }

I need this code on iOS to return only the specific ID I'm specifying in .child().

But I've already realized it's an iOS SDK issue.

1 Answer 1

2

Unfortunately this is one of a number of known issues with the implementation of getData() on iOS (#12168, #14137, #12965, #8286). Since the Flutter SDK wraps the native SDKs, it is affected by this issue too. The Android and web SDKs don't have the same problem, so on Flutter, you indeed won't encounter the problem on those platforms.

Given how long this has been known-yet-unfixed, it's probably best to work around the issue by calling .once() instead of get() as shown in reading data once with an observer. The behavior is going to be pretty much the same, but this older API has fewer known issues.

Also see:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.