0

我正在尝试将此列表作为流返回,但出现以下错误:

1. The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.

2. A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>'.

3. A value of type 'List<dynamic>?' can't be returned from the method 'userBookmarkedCourses' because it has a return type of 'Stream<List<dynamic>?>'. 

下面的清单是导致这些错误的代码:

  Stream<List<dynamic>?> userBookmarkedCourses() {
    print("User Id within User Provider: $userId");
    QuerySnapshot userSnapshot;
    try {
      if (userId != null) {
        userSnapshot = FirebaseFirestore.instance.collection('users').where('userID', isEqualTo: userId).get();
        if (userSnapshot.docs.isNotEmpty) {
          return coursesBookmarkedList = userSnapshot.docs[0].get('coursesBookmarked');
        }
        return coursesBookmarkedList = [];
      }
    } catch (error) {
      coursesBookmarkedList = [];
    }
  }

我还想知道如何在 中声明它MultiProviders,我是否使用Provider.of

我是 Streams 的新手。这个错误的原因是什么?

4

1 回答 1

1

1-你必须在 catch 块中返回一些东西。因为方法 userBookmarkedCourses 有一个返回类型。

2-您必须将 QuerySnapshot<Map<String, dynamic>> 设置为userSnapshot的类型。此外,您必须在FirebaseFirestore.instance.collection('users').where('userID', isEqualTo: userId).get();之前放置await

3-方法的返回类型是Stream<List?>。但是,您要返回List<dynamic>?. 将方法的返回类型更改为List<dynamic>?或返回一个变量,它是Stream<List?>

于 2021-08-30T12:24:05.950 回答