不熟悉。我试图从firestore中获取项目以在应用程序的Listview中显示,但得到“构建函数返回null。有问题的小部件是:StreamBuilder,构建函数绝不能返回null”。我只想要listview中显示的firstore列表“ post”
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Post App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Color(0xff543b7a),
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(FontAwesomeIcons.hamburger),
),
),
body: StreamBuilder(
stream: Firestore.instance.collection('post').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
Text('Loading');
} else {
return ListView.builder(
itemCount: snapshot.data.document.length,
itemBuilder: (context, index) {
DocumentSnapshot myPost = snapshot.data.documents[index];
return Stack(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: 350.0,
child: Padding(
padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
child: Material(
color: Colors.white,
elevation: 14.0,
shadowColor: Color(0x802196f3),
child: Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: 200.0,
child: Image.network(
'${myPost['image']}',
fit: BoxFit.fill,
),
),
SizedBox(
height: 10.0,
),
Text('${myPost['title']}'),
SizedBox(
height: 10.0,
),
Text('${myPost['subtitle']}'),
],
),
),
),
)
],
);
},
);
},
},
),
);
}
}
[enter image description here][1]
[1]: https://i.stack.imgur.com/QeSyi.png
一个构建函数返回空值。有问题的小部件是:StreamBuilder。构建函数绝不能返回空值。
答案 0 :(得分:0)
您错过了退货:
builder: (context, snapshot) {
if (!snapshot.hasData) {
Text('Loading'); // <---- no return here
} else {
return ListView.builder(
itemCount: snapshot.data.documents.length, // <---- documents here
itemBuilder: (context, index) {
DocumentSnapshot myPost = snapshot.data.documents[index];