简体   繁体   English

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

[英]Having trouble getting DataSnapshot from database and getting null values

I am trying to view a users story post by pressing their story button, navigating me to the full page storyView to see the images.我试图通过按下他们的故事按钮来查看用户的故事帖子,将我导航到整页故事视图以查看图像。 I think I am having trouble passing the data to the next page or querying the data from the database.我想我在将数据传递到下一页或从数据库查询数据时遇到了问题。 I'm not sure where I went wrong.我不确定我哪里出错了。

This is the error I'm getting:这是我得到的错误:

The following _TypeError was thrown building Builder: type '(DataSnapshot) => Null' is not a subtype of type '(dynamic) => dynamic' of 'f''以下 _TypeError 被抛出构建 Builder: type '(DataSnapshot) => Null' is not a subtype of type '(dynamic) => dynamic' of 'f''

This is where I'm calling the data to be retrieved on the actual page where you would view the image.这是我调用要在您查看图像的实际页面上检索的数据的地方。 The error is pointing to the crudMethods.getStoryListData() line错误指向 crudMethods.getStoryListData() 行

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:link_me/services/crud.dart';
import "package:story_view/story_view.dart";

class StoryPageView extends StatefulWidget {
  final String currentUser;
  final String storyUrl;

  StoryPageView({this.currentUser, this.storyUrl});

  @override
  _StoryPageViewState createState() => _StoryPageViewState();
}

class _StoryPageViewState extends State<StoryPageView> {
  CrudMethods crudMethods = new CrudMethods();
  List<String> itemList = new List();
  final StoryController controller = StoryController();
  

  QuerySnapshot storySnapshot;

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

    crudMethods.getStoryListData().then((DataSnapshot snapshot) {
      //storySnapshot = result;
      var urlData = snapshot.value;
      //itemList.clear();
      urlData.forEach((key, value) {
        itemList.add(value['storyUrl']);
      });

      setState(() {
        print(itemList.length);
      });
    });
  }

 loadStory() {
    return ListView(
      children: <Widget>[
        itemList == null ? CircularProgressIndicator()
        : Container(
          child: StoryView(
             storyItems: [
              for (var i in itemList) show(i)
            ], 
            controller: controller,
            progressPosition: ProgressPosition.top,
            repeat: false,
            ),
        ),
      ],
    );
  }

  show(String urlData) {
    return StoryItem.pageImage(
      url: urlData, 
      controller: controller,
      );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        margin: EdgeInsets.all(8.0),
        child: Column(
          children:<Widget>[
            Padding(
              padding: EdgeInsets.fromLTRB(0, 5, 5, 5),
              child: Container(
                height: MediaQuery.of(context).size.width,
                width: MediaQuery.of(context).size.height,
                child: loadStory(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

This is how I am navigating to that page from the main story list page that show all users who have posted a new story这就是我从主故事列表页面导航到该页面的方式,该页面显示发布了新故事的所有用户

onTap: () => Navigator.push(context,
                  MaterialPageRoute(builder: (context) => StoryPageView(currentUser: currentUser.id,))),

And this is from my utility file I'm using to call the data这是来自我用来调用数据的实用程序文件

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

I think the culprit is your getStoryListData , redefine it like this :我认为罪魁祸首是你的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;

  }

Edit:编辑:

Redifine your initState like this :像这样重新定义你的initState

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

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

and your getStoryListData like this :和你的getStoryListData像这样:


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;

  }

Furthermore , you cant use snapshot.value because QuerySnapshot dont have any property value .此外,您不能使用snapshot.value因为QuerySnapshot没有任何属性value You have to use snapshot.docs which returns List<QueryDocumentSnapshot> .Iterate over the List and use .data() method to get a Map<String,dynamic> which has all the data you want .Update your code accordingly.你必须使用snapshot.docs返回List<QueryDocumentSnapshot>在清单和使用.Iterate .data()方法得到一个Map<String,dynamic>它拥有所有的数据你要相应.Update你的代码。

You are using future in the UI but returning stream from the getStoryListData() so you have to return future as Future getStoryListData() async { Querysnapshot snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).您在 UI 中使用 future 但从 getStoryListData() 返回流,因此您必须将 future 作为 Future getStoryListData() async { Querysnapshot snapshot= storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("时间戳”,降序:真)。 get();得到(); return snapshot;返回快照;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM