Firebase 从数据库获取DataSnapshot和获取空值时遇到问题

Firebase 从数据库获取DataSnapshot和获取空值时遇到问题,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我试图通过按用户的故事按钮来查看用户的故事帖子,将我导航到完整页面的故事视图以查看图像。我想我在将数据传递到下一页或从数据库查询数据时遇到了问题。我不确定哪里出了错 这就是我得到的错误: 建筑生成器引发了以下类型错误: type'(DataSnapshot)=>Null'不是'f'的type'(dynamic)=>dynamic'的子类型 在这里,我调用要在实际页面上检索的数据,您将在该页面上查看图像。错误指向crudMethods.getStoryListData()行 这是我用来调用数据的实

我试图通过按用户的故事按钮来查看用户的故事帖子,将我导航到完整页面的故事视图以查看图像。我想我在将数据传递到下一页或从数据库查询数据时遇到了问题。我不确定哪里出了错

这就是我得到的错误:

建筑生成器引发了以下类型错误: type'(DataSnapshot)=>Null'不是'f'的type'(dynamic)=>dynamic'的子类型

在这里,我调用要在实际页面上检索的数据,您将在该页面上查看图像。错误指向crudMethods.getStoryListData()行

这是我用来调用数据的实用程序文件

getStoryListData() async {
    return storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();
  }

我认为罪魁祸首是您的
getStoryListData
,请将其重新定义为:

Future<DataSnapshot> getStoryListData() async {
  DataSnapshot snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.value); //to know whether you are making successful queries or not .
return snapshot;

  }
@override
  void initState() {
    super.initState();
    CrudMethods crudMethods = new CrudMethods();

    crudMethods.getStoryListData().then((Stream<QuerySnapshot> snapshot) {
.
.
//rest of the code

Future<Stream<QuerySnapshot>> getStoryListData() async {
  Stream<QuerySnapshot> snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.docs.toString()); //to know whether you are making successful queries or not .
return snapshot;

  }

您的
getStoryListData
如下所示:

Future<DataSnapshot> getStoryListData() async {
  DataSnapshot snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.value); //to know whether you are making successful queries or not .
return snapshot;

  }
@override
  void initState() {
    super.initState();
    CrudMethods crudMethods = new CrudMethods();

    crudMethods.getStoryListData().then((Stream<QuerySnapshot> snapshot) {
.
.
//rest of the code

Future<Stream<QuerySnapshot>> getStoryListData() async {
  Stream<QuerySnapshot> snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.docs.toString()); //to know whether you are making successful queries or not .
return snapshot;

  }


未来的getStoryListData()异步{
Stream snapshot=storyPostReference.document(currentUser.id).collection(“storyPostItems”).orderBy(“时间戳”,降序:true).snapshots();
打印(snapshot.docs.toString());//以了解是否正在进行成功的查询。
返回快照;
}

此外,您不能使用
snapshot.value
,因为
QuerySnapshot
没有任何属性
value
。你必须使用
snapshot.docs
,它返回
List
。在列表上迭代并使用
.data()
方法获得一个
映射,该映射包含你想要的所有数据。相应地更新你的代码。

我认为罪魁祸首是你的
getStoryListData
,重新定义如下:

Future<DataSnapshot> getStoryListData() async {
  DataSnapshot snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.value); //to know whether you are making successful queries or not .
return snapshot;

  }
@override
  void initState() {
    super.initState();
    CrudMethods crudMethods = new CrudMethods();

    crudMethods.getStoryListData().then((Stream<QuerySnapshot> snapshot) {
.
.
//rest of the code

Future<Stream<QuerySnapshot>> getStoryListData() async {
  Stream<QuerySnapshot> snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.docs.toString()); //to know whether you are making successful queries or not .
return snapshot;

  }

您的
getStoryListData
如下所示:

Future<DataSnapshot> getStoryListData() async {
  DataSnapshot snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.value); //to know whether you are making successful queries or not .
return snapshot;

  }
@override
  void initState() {
    super.initState();
    CrudMethods crudMethods = new CrudMethods();

    crudMethods.getStoryListData().then((Stream<QuerySnapshot> snapshot) {
.
.
//rest of the code

Future<Stream<QuerySnapshot>> getStoryListData() async {
  Stream<QuerySnapshot> snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();

print(snapshot.docs.toString()); //to know whether you are making successful queries or not .
return snapshot;

  }


未来的getStoryListData()异步{
Stream snapshot=storyPostReference.document(currentUser.id).collection(“storyPostItems”).orderBy(“时间戳”,降序:true).snapshots();
打印(snapshot.docs.toString());//以了解是否正在进行成功的查询。
返回快照;
}

此外,您不能使用
snapshot.value
,因为
QuerySnapshot
没有任何属性
value
。您必须使用
快照.docs
返回
列表
。迭代列表并使用
.data()
方法获得包含所有所需数据的
映射。相应地更新您的代码。

您在UI中使用的是future,但从getStoryListData()返回流,因此您必须返回future 未来的getStoryListData()异步{ Querysnapshot snapshot=storyPostReference.document(currentUser.id).collection(“storyPostItems”).orderBy(“时间戳”,降序:true).get();
return snapshot;

您在UI中使用的是future,但从getStoryListData()返回流,因此必须将future作为 未来的getStoryListData()异步{ Querysnapshot snapshot=storyPostReference.document(currentUser.id).collection(“storyPostItems”).orderBy(“时间戳”,降序:true).get();
return snapshot;

我尝试应用此快照,但它给了我红线错误
无法将“Stream”类型的值分配给“DataSnapshot”类型的变量。请尝试更改变量的类型,或将右侧类型强制转换为“DataSnapshot”。
我被迫将其更改为类似于
Future getUserStoryData()async的内容{Stream snapshot=storyPostReference.document(currentUser.id).collection(“storyPostItems”).orderBy(“时间戳”,降序:true).snapshots();return snapshot;}
但是我还是得到了同样的结果。你觉得它有用吗?我试图应用这个方法,但它给了我红线错误
类型为“Stream”的值不能分配给类型为“DataSnapshot”的变量。尝试更改变量的类型,或者将右侧类型强制转换为“DataSnapshot”。
我被迫将其更改为其他类型ke
Future getUserStoryData()async{Stream snapshot=storyPostReference.document(currentUser.id).collection(“storyPostItems”).orderBy(“timestamp”,descending:true).snapshots();return snapshot;}
但是我仍然得到了相同的结果,你觉得有用吗?