Flutter-Firebase await return after stream.listen() finish

446

Solution 1

So you can try using .onDone after stream. So if the stream ends, onDone block will run.

 stream.listen((List<DocumentSnapshot> documentList) {

}).onDone((){
 //run here
});

Solution 2

you don't need a listen to get only the first-time data from the stream.

After initializing your stream, do following things:

List<DocumentSnapshot> documentList = await stream.first;
// Do your operations here
return newResult;
Share:
446
FLTY
Author by

FLTY

Updated on January 01, 2023

Comments

  • FLTY
    FLTY over 1 year

    Flutter Stream Issue I am trying to filter my database query by geohash, so I'm using Geoflutterfire, which returns a Stream. I tried to convert data from each DocumentSnapshot inside Stream<List<>> into a List. It works, but the problem is that the return happens before the actual finish of the stream.listen(). How can I delay the return to return the stream.listen result? I tried using await for(...), but it errors.

    import 'dart:async';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter_redux/flutter_redux.dart';
    import 'package:geoflutterfire/geoflutterfire.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:firebase_storage/firebase_storage.dart';
    import 'package:google_maps_flutter/google_maps_flutter.dart';
    import 'package:uerto/models/index.dart';
    
    class SearchApi {
      const SearchApi({required FirebaseAuth auth, required FirebaseFirestore firestore, required FirebaseStorage storage, required Geoflutterfire geo})
          : _auth = auth,
            _firestore = firestore,
            _storage = storage,
            _geo = geo;
    
      final FirebaseAuth _auth;
      final FirebaseFirestore _firestore;
      final FirebaseStorage _storage;
      final Geoflutterfire _geo;
    
      Future<List<AppClient>> getClientList(LatLng location, String category, String subCategory, double radius, int limit) async{
    
        final List<AppClient> newResult = <AppClient>[];
        final GeoFirePoint center = _geo.point(latitude: location.latitude, longitude: location.longitude);
        final Query<Map<String, dynamic>> collectionReference = _firestore.collection('London$category/$subCategory/UID').limit(limit);
        const String field = 'position';
        final Stream<List<DocumentSnapshot<Map<String, dynamic>>>> stream = _geo.collection(collectionRef: collectionReference).within(center: center, radius: radius, field: field);
        // ignore: always_specify_types
        stream.listen((List<DocumentSnapshot> documentList) {
          // ignore: always_specify_types, avoid_function_literals_in_foreach_calls
          documentList.forEach((DocumentSnapshot document) async {
            ///print(document.data());
            final SearchUid searchUid = SearchUid.fromJson(document.data());
            //print(searchUid.uid);
            final DocumentSnapshot<Map<String, dynamic>> client = await _firestore.collection('clients').doc(searchUid.uid).get();
            final AppClient clientData = AppClient.fromJson(client.data());
            print(clientData);
            newResult.add(clientData);
          });
        });
        await for(List<DocumentSnapshot> documentList in stream){
          return newResult;
        }
    
      }
    }
    

    enter image description here

    • Chetan Goyal
      Chetan Goyal over 2 years
      Do you want to return only the first result from your stream?
    • FLTY
      FLTY over 2 years
      @ChetanGoyal yes, exactly
    • Chetan Goyal
      Chetan Goyal over 2 years
      okay. I am giving an appropriate answer for it now.
    • FLTY
      FLTY over 2 years
      thanks for your time!
    • Chetan Goyal
      Chetan Goyal over 2 years
      No problem. I have posted an answer. Let me know if you face any other problem while using it.
  • FLTY
    FLTY over 2 years
    its still the same error
  • FLTY
    FLTY over 2 years
    Well i changed my code with your and its still the same problem
  • Chetan Goyal
    Chetan Goyal over 2 years
    @FLTY I have edited my answer. Is it still not working?
  • FLTY
    FLTY over 2 years
    no it doesnt work either.. i have added await before you told me (cause an error popped)..
  • Chetan Goyal
    Chetan Goyal over 2 years
    Can you please show the error you received?
  • Toto
    Toto over 2 years