Search code examples
fluttergoogle-cloud-firestorestream-builder

flutter orderBy("time", "asc") not working


i am using nested stream and future to get 2 sets of data, but cannot sort the second set of data by time

Tried both but still not working.. why???

Error: Getter not found: 'Direction'.
                               .orderBy("time", Query.Direction.DESCENDING)
                                                      ^^^^^^^^^

Error: Too many positional arguments: 1 allowed, but 2 found.
Try removing the extra positional arguments.
                               .orderBy("time", 'asc')
                                       ^

full code below:

return StreamBuilder(
      stream: A,
      builder: (context, snapshot) {
        return snapshot.hasData
            ? ListView.builder(
                ...
                  return
                         FutureBuilder(
                           future: FirebaseFirestore.instance
                           .collection("some path here")
                           .orderBy("time", Query.Direction.DESCENDING)
                           .get(),,

                           builder: (context, snapshot2) {
                           if (snapshot2.hasData) {
                                   if (snapshot2.data!=null) {
                                   return DisplayTile(
                                     ...
                                   );
                                   }
                           }
                           return CircularProgressIndicator();
                           }
                           );
                })
            : Container();
      },
    );

Solution

  • orderBy has 1 named parameter called descending of bool type to pass order.

    Example from docs:

    FirebaseFirestore.instance
      .collection('users')
      .orderBy('age', descending: true) // here
      .get()
      .then(...);