我试图通过按用户的故事按钮来查看用户故事,将我导航到整页的StoryView以查看图像。我认为我无法将数据传递到下一页或从数据库查询数据。我不确定哪里出了问题。
这是我得到的错误:
在构建Builder时引发了以下_TypeError: 类型'((DataSnapshot)=> Null'不是'f'类型'(dynamic)=> dynamic'的子类型
这是我在要查看图像的实际页面上调用要检索的数据的地方。错误指向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(),
),
),
],
),
),
);
}
}
这就是我从主故事列表页面导航至该页面的过程,该页面显示了发布新故事的所有用户
onTap: () => Navigator.push(context,
MaterialPageRoute(builder: (context) => StoryPageView(currentUser: currentUser.id,))),
这是从我用来调用数据的实用程序文件中
getStoryListData() async {
return storyPostReference.document(currentUser.id).collection("storyPostItems").orderBy("timestamp", descending: true).snapshots();
}
答案 0 :(得分:1)
我认为罪魁祸首是您的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;
}
编辑:
像这样重新命名您的initState
:
@override
void initState() {
super.initState();
CrudMethods crudMethods = new CrudMethods();
crudMethods.getStoryListData().then((Stream<QuerySnapshot> snapshot) {
.
.
//rest of the code
和您的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;
}
此外,您不能使用snapshot.value
,因为QuerySnapshot
没有任何属性value
。您必须使用snapshot.docs
返回List<QueryDocumentSnapshot>
。在List上进行迭代,并使用.data()
方法获取包含所有所需数据的Map<String,dynamic>
。相应地更新代码。
答案 1 :(得分:1)
您正在UI中使用Future,但是从getStoryListData()返回流,因此您必须以 未来的getStoryListData()异步{ Querysnapshot快照= storyPostReference.document(currentUser.id).collection(“ storyPostItems”)。orderBy(“ timestamp”,降序:true)。得到(); 返回快照;