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;
Author by
FLTY
Updated on January 01, 2023Comments
-
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 eachDocumentSnapshot
insideStream<List<>>
into aList
. It works, but the problem is that thereturn
happens before the actual finish of thestream.listen()
. How can I delay thereturn
to return thestream.listen
result? I tried usingawait 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; } } }
-
Chetan Goyal over 2 yearsDo you want to return only the first result from your stream?
-
FLTY over 2 years@ChetanGoyal yes, exactly
-
Chetan Goyal over 2 yearsokay. I am giving an appropriate answer for it now.
-
FLTY over 2 yearsthanks for your time!
-
Chetan Goyal over 2 yearsNo problem. I have posted an answer. Let me know if you face any other problem while using it.
-
-
FLTY over 2 yearsits still the same error
-
FLTY over 2 yearsWell i changed my code with your and its still the same problem
-
Chetan Goyal over 2 years@FLTY I have edited my answer. Is it still not working?
-
FLTY over 2 yearsno it doesnt work either.. i have added await before you told me (cause an error popped)..
-
Chetan Goyal over 2 yearsCan you please show the error you received?
-
Toto over 2 years