Home > Back-end >  Flutter: A value of type 'Stream<List<object>>' can't be returned from th
Flutter: A value of type 'Stream<List<object>>' can't be returned from th

Time:10-13

I'm getting error in returning the response. I want the type to be List<ObjectModel> but it is returning Stream<List<ObjectModel>>

I tried response as List<ObjectModel> but it is not solving the error. Please help

List<ZohoModel> getCheckInHistory() {
  var response =  FirebaseFirestore.instance
      .collection("checkIn")
      .snapshots()
      .map((snapshot) =>
          snapshot.docs.map((doc) => ZohoModel.fromJson(doc.data())).toList());
  print(" the response for check-In ${response}");
  return response ;
}

the result of printing "response" is :

Instance of '_MapStream<QuerySnapshot<Map<String, dynamic>>, List<ZohoModel>>'

CodePudding user response:

I think it would be better return Stream for most usecase,

 Stream<List<ZohoModel>> getCheckInHistory() { ..

To get the data you can use

getCheckInHistory().last

If you still like to get List, you can convert it to Future method

  Future<List<ZohoModel>> getCheckInHistory()async {
    var response = await FirebaseFirestore.instance
      ....
    return response.last;
  }

CodePudding user response:

if You're working with stream builder, make your function return type Stream<List>? and return response.

Stream<List<ZohoModel>> getCheckInHistory() {
      var response =  FirebaseFirestore.instance
          .collection("checkIn")
          .snapshots()
          .map((snapshot) =>
              snapshot.docs.map((doc) => ZohoModel.fromJson(doc.data())).toList());
      print(" the response for check-In ${response}");
      return response ;
    }

alternatively,

declare list like this outside the function

 Stream<List<QuerySnapshot>>? response;

then get response in the function without return, using notifyListeners();

getCheckInHistory() {
   response =  FirebaseFirestore.instance
      .collection("checkIn")
      .snapshots()
      .map((snapshot) =>
          snapshot.docs.map((doc) => ZohoModel.fromJson(doc.data())).toList());
  print(" the response for check-In ${response}");
  notifyListeners();
}
  • Related