我想在详细信息页面中显示“内容”,但是“内容”是一个数组。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Pese App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Pese App'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListPage(),
);
}
}
class ListPage extends StatefulWidget {
@override
_ListPageState createState() => _ListPageState();
}
class _ListPageState extends State<ListPage> {
Future _data;
Future getPosts() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("pese").getDocuments();
return qn.documents;
}
@override
void initState() {
super.initState();
_data = getPosts(); //getPosts stored in _data
}
navigateToDetail(DocumentSnapshot post) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
post: post,
)));
}
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder(
future: _data,
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading..."),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) {
return Container(
child: Card(
child: ListTile(
title: Text(snapshot.data[index].data['title']),
subtitle:
Text(snapshot.data[index].data['subtitle']),
onTap: () =>
navigateToDetail(snapshot.data[index]),
),
),
);
});
}
}),
);
}
}
class DetailPage extends StatefulWidget {
final DocumentSnapshot post;
DetailPage({this.post});
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.post.data["title"]),
),
body: Container( //container
child: Card(
child: ListTile(
因此,每次我运行该程序并尝试导航到详细信息页面时,总是收到错误“断言失败:第230行pos 15:'data!= null':不正确”。 如果我仅将“ content”作为字符串放置,则代码可以正常工作,但是当我在firebase中将“ content”作为数组时,会出现错误。
subtitle: Text(widget.post.data['content']), //where the error is
)),
),
);
}
}