Home > Mobile >  Flutter: stuck getting collection from Firestore
Flutter: stuck getting collection from Firestore

Time:12-19

DocumentReference ref = FirebaseFirestore.instance
        .collection('invoices')
        .doc(authService.getUser().uid); // or non existing ID

    // here works if I return {}

    final doc = await ref.get();
    final data = doc.data();

When trying locally either with simulator or with real device (connected by usb) works fine, but in released app, the await takes for ever.

This was also working ok before updating to flutter 2.8 but now it fails only on Android

CodePudding user response:

Reposting my comments as they seemed to have helped:

You should handle possible exceptions. The get method returns an instance of Future, which may complete with an error. When awaiting it, it may throw.

CodePudding user response:

I put everything inside a trycatch

try { DocumentReference ref = FirebaseFirestore.instance .collection('invoices') .doc(authService.getUser().uid);

      final doc = await ref.get();
      final data = doc.data();

      Map savedCompanyInfo = data != null ? data as Map<String, dynamic> : {};
      myRents.companyInfo = savedCompanyInfo;

      return savedCompanyInfo;
    } catch (e) {
      return {};
    }
  • Related