skip to Main Content

My function looks like this:

 Stream<QuerySnapshot> getAllLists() {
    late String temp = "name";

    getUserSortMethod().then((value) => {
          temp = value.toString(),
          // temp is now "prio"
        });

    return firestore
        .collection('lists')
        .where('editors', arrayContains: currentUser.email)
        .orderBy(temp, descending: false)
        .snapshots();
  }

I need to wait for the result of getUserSortMethod() to use it in return. How can I use return and before that wait for result of getUserSortMethod function?

Tried to change main function to async return to

 Future<Stream<QuerySnapshot<Object?>>> but get not working.

2

Answers


  1. Chosen as BEST ANSWER

    this works very well:

      Stream<QuerySnapshot> getAllLinks(String listName) async* {
    print('### LLFireDataSource::getAllLinks:' + currentUser.email.toString());
    var sort = await getUserSortMethod();
    yield* firestore
        .collection('items')
        .where('list', isEqualTo: listName)
        .orderBy(sort, descending: false)
        .snapshots();
    

    }


  2. You are using async method but the method declaration doesn’t mention as async. In your sample code you don’t have await due to that firestore order will be done by name always. Because before completing the async operation it will return the snapshot.

    Future<Stream<QuerySnapshot> getAllLists() async {
        late String temp = "name";
    
        // use await here because you need temp value updated by getUserSortMethod async function.
        temp = await getUserSortMethod();
    
        return firestore
            .collection('lists')
            .where('editors', arrayContains: currentUser.email)
            .orderBy(temp, descending: false)
            .snapshots();
      }
    

    Otherwise run the getUserSortMethod async function then pass the result to getAllList Function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search