Implementing Composite Index with FlutterFire

Viewed 39

Trying to create a composite index so I can complete the following QuerySnapshot:

final Stream<QuerySnapshot> _tasksStream = FirebaseFirestore.instance
        .collection('tasks')
        .orderBy('duedate', descending: true)
        .where('mocUID', isEqualTo: widget.uid)
        .where('type', isEqualTo: 'General')
        .snapshots();

The terminal in Android studio isn't returning a link to create said index as promised in the youtube videos, so I created it manually:

Firebase console snip

But my query continues to return 'Something went wrong'.

    return StreamBuilder<QuerySnapshot>(
      stream: _tasksStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }
1 Answers

The index looks good for the query at first glance, but it's hard to be certain.

It's much more helpful if you log the error with:

if (snapshot.hasError) {
  print('Error in Firestore query: ${snapshot.error}');
  return const Text('Something went wrong');
}

At the very least that will tell you want it going wrong. If the problem is caused by a missing index, it'll log an error message with a direct link to the Firebase console to create the necessary index - with all fields prepopulated.

Related