Flutter Flatter firestore将文档数据推送到详细信息屏幕
我需要点击列表,打开一个新的详细信息页面。我到处寻找如何做到这一点的例子,我发现了很多。但是,我的数据结构不同,即使我的代码没有错误,它也不会填充详细信息页面。以下是我的工作内容:Flutter Flatter firestore将文档数据推送到详细信息屏幕,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我需要点击列表,打开一个新的详细信息页面。我到处寻找如何做到这一点的例子,我发现了很多。但是,我的数据结构不同,即使我的代码没有错误,它也不会填充详细信息页面。以下是我的工作内容: //page 1 child: ListView.builder( itemCount: _documents == null ? 0 : _documents.length,
//page 1
child: ListView.builder(
itemCount:
_documents == null ? 0 : _documents.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
"assets/images/roomfield1.png"),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter),
color:
Theme.of(context).canvasColor,
),
child: ListTile(
title: Text(_documents[index]
['title']),
subtitle: Text(_documents[index]
["description"]),
trailing: Text(_documents[index]
['citystate']),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DetailPage(_documents[index])));
})));
},
以下是详细信息页面:
// details page
import 'package:flutter/material.dart';
class DetailPage extends StatefulWidget {
DetailPage(_documents, {Key key,}) : super(key: key);
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
var _documents;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('Details Page'),
),
body: ListView.builder(
itemCount: _documents == null ? 0 : _documents.length,
itemBuilder: (BuildContext context,[index]) {
return ListTile(
title: Text(_documents[index]['title']),
);
}));}}
//详细信息页
进口“包装:颤振/材料.省道”;
类DetailPage扩展StatefulWidget{
DetailPage(_documents,{Key,}):super(Key:Key);
@凌驾
_DetailPageState createState()=>\u DetailPageState();
}
类_DetailPageState扩展状态{
var_文件;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
标题:文本(“详细信息页”),
),
正文:ListView.builder(
itemCount:_documents==null?0:_documents.length,
itemBuilder:(构建上下文,[索引]){
返回列表块(
标题:文本(_文档[索引]['title']),
);
}));}}
我的文档使用documentId。我尝试传递id,然后在流生成器中使用它,但这也不起作用。我认为我的代码很接近,但我不知道哪里出了问题。此外,我不能改变数据的结构,因为它是使用这种格式的所有地方。我猜我错过了一些简单的东西,可能是详细信息页面上的构造函数,但我不确定…任何见解都值得欣赏。您在类的顶部创建了一个名为
\u documents
的变量。但是,要在DetailPage
类中检索实际\u文档
变量的值,需要使用小部件.documents
。我在下面提供了正确的代码供您使用
另外,我不知道为什么要在DetailPage
类中使用StatefulWidget
,因为您似乎没有做任何需要状态管理的事情。你可以很容易地把它变成一个无状态小部件
,但也许还有更多我看不到的代码
// details page
import 'package:flutter/material.dart';
class DetailPage extends StatefulWidget {
final var documents; // created new final variable that will be initialized by the constructor
DetailPage(this.documents);
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
//var _documents; // Don't use this
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('Details Page'),
),
body: ListView.builder(
itemCount: widget.documents == null ? 0 : widget.documents.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(widget.documents[index]['title']), // don't know why you need to specify the index since you already did in your other classs
);
}));}}
//详细信息页
进口“包装:颤振/材料.省道”;
类DetailPage扩展StatefulWidget{
final var documents;//创建了将由构造函数初始化的新final变量
详细页(本文件);
@凌驾
_DetailPageState createState()=>\u DetailPageState();
}
类_DetailPageState扩展状态{
//var _documents;//不要使用此
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
标题:文本(“详细信息页”),
),
正文:ListView.builder(
itemCount:widget.documents==null?0:widget.documents.length,
itemBuilder:(上下文,索引){
返回列表块(
title:Text(widget.documents[index]['title']),//不知道为什么需要指定索引,因为您已经在其他类中指定了索引
);
}));}}
谢谢。而且,你是对的,我不需要索引。