Home > Back-end >  A value of type 'Stream<List<AppUserData>>' can't be returned from the fu
A value of type 'Stream<List<AppUserData>>' can't be returned from the fu

Time:12-25

I am building a Chat App in flutter with firebase. So I created a file where I connect the firebase database to my App :

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:socket/services/user.dart';

class DatabaseService {
  final String? uid;

  DatabaseService({this.uid});
  final CollectionReference userCollection =
      FirebaseFirestore.instance.collection("users");

  Future<void> saveUser(String name) async {
    return await userCollection.doc(uid).set({'name': name});
  }

  AppUserData _userFromSnapshot(DocumentSnapshot snapshot) {
    return AppUserData(name: snapshot['name'], uid: uid);
  }

  Stream<AppUserData> get user {
    return userCollection.doc(uid).snapshots().map(_userFromSnapshot);
  }

  List<AppUserData> _userListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc) {
      return _userFromSnapshot(doc);
    }) as List<AppUserData>;
  }

  Future<List<AppUserData>> get users {
    return userCollection.snapshots().map(_userListFromSnapshot);
  }
}

Here's the User.dart :

    class AppUser {
  final String? uid;
  AppUser({this.uid});
}

class AppUserData {
  final String? uid;
  final String name;

  AppUserData({this.uid, required this.name});
}

However, I get the error

A value of type 'Stream<List<AppUserData>>' can't be returned from the function 'users' because it has a return type of 'Future<List<AppUserData>>'.

On the fonction Future<List<AppUserData>> get users. I am not really experimented in databases and providers so I don't really understand what this error mean. Can you help me ? Thank you in advance.

EDIT: I changer Future<List<AppUserData>> to Stream<List<AppUserData>> but then I got the error

An exception was throw by _MapStream<QuerySnapshot<Map<String, dynamic>>, List> listened by Can you Help ?

CodePudding user response:

it's just a type specification problemn, the snapshots() is a Stream, so trying to return it, the method return should match it, change Future<List<AppUserData>> with Stream<List<AppUserData>> :

  Stream<List<AppUserData>> get users {
   return userCollection.snapshots().map(_userListFromSnapshot);
  }

CodePudding user response:

If you want to extract the information of the collection users you may not need to use snapshot(). You coudl use get() instead.

userColletion.get()
            .then((snapshot) {
          for (DocumentSnapshot doc in snapshot.docs) {
            //do something
          }
        });     

When you use snapshot() you create a Stream as you see in the error log. Using get you are getting the documents of the collection once. If you need to keep watching you may find reasonable the use of the stream builder, or a list of items from a Stream of data. I'm not really sure the use you are giving to this class.

Here you have a question related: What is the difference between getDocuments() and snapshots() in Firestore?

  • Related