Fetching data from firebase error happen flutter

Viewed 19

while building whatsapp clone status uploading to database perfectly done , no issues in that case . But while displaying from DB to screen error found :- I/flutter (11808): RangeError (index): Invalid value: Valid value range is empty: 0

code of getstatus function

Future<List> getStatus(BuildContext context) async { List statusData = [];

try {
  List<Contact> contacts = [];
  if (await FlutterContacts.requestPermission()) {
    contacts = await FlutterContacts.getContacts(withProperties: true);
  }

  for (int i = 0; i < contacts.length; i++) {
    var statusesSnapshot = await firestore
        .collection('status')
        .where(
          'phoneNumber',
          isEqualTo: contacts[i].phones[0].number.replaceAll(
                ' ',
                '',
              ),
        )
        .where(
          'createdAt',
          isGreaterThan: DateTime.now()
              .subtract(const Duration(hours: 24))
              .microsecondsSinceEpoch,
        )
        .get();

    for (var tempData in statusesSnapshot.docs) {
      Status tempStatus = Status.fromMap(tempData.data());
      if (tempStatus.whoCanSee.contains(auth.currentUser!.uid)) {
        statusData.add(tempStatus);
      }
    }
  }
} catch (e) {
  if (kDebugMode) print(e);
  showSnackBar(context: context, content: e.toString());
}
return statusData;

}

0 Answers
Related