我一直收到此错误,但我似乎无法弄清楚哪个部分返回null,我什至试图返回一个空容器,以防万一,我不确定我要去哪里,在没有此错误的情况下通常会显示此错误Firestore中的数据
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:provider/provider.dart';
import 'package:hotel_search/model.dart';
class BookScreen extends StatefulWidget {
@override
book createState() => book();
}
class book extends State<BookScreen> {
@override
void initState() {
super.initState();
}
String userId;
checkDatabase() {
var docSnapshot = FirebaseFirestore.instance
.collection('bookings')
.where('customer', isEqualTo: userId)
.snapshots();
return docSnapshot;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
bottom: TabBar(
labelColor: Colors.black,
indicatorColor: Color(0xff008d4b),
labelStyle: TextStyle(fontSize: 13, fontFamily: 'Montserrat'),
tabs: [
Tab(
text: "Upcoming",
),
Tab(text: "Cancelled"),
Tab(text: "Past"),
],
),
title: Text('My Bookings',
style: TextStyle(
color: Colors.black,
fontFamily: 'Montserrat',
fontSize: 20,
)),
),
body: TabBarView(
children: [
Container(
color: Colors.grey[100],
child: StreamBuilder(
stream: checkDatabase(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data.documents.isEmpty) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<MyModel>(
builder: (context, myModel, children) {
userId = myModel.customer;
return Container(
child: Container(
child: Image.asset(
"assets/images/sunbird_white.png",
width: 300,
height: 80,
fit: BoxFit.contain,
)));
}),
SizedBox(height: 25),
Container(
width: MediaQuery.of(context).size.width,
child: Center(
child: Text(
"You currently dont have \n any upcoming stays at our Hotels",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
fontFamily: 'Montserrat'),
)))
],
);
} else if (snapshot.data.documents.isNotEmpty) {
return Container(width: 0, height: 0);
}
}
})),
Container(
color: Colors.grey[100],
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Container(
child: Image.asset(
"assets/images/sunbird_white.png",
width: 300,
height: 80,
fit: BoxFit.contain,
))),
SizedBox(height: 25),
Container(
width: MediaQuery.of(context).size.width,
child: Center(
child: Text(
"You currently dont have \n any cancelled stays at our Hotels",
textAlign: TextAlign.center,
style:
TextStyle(fontSize: 14, fontFamily: 'Montserrat'),
)))
],
),
),
Container(
color: Colors.grey[100],
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Container(
child: Image.asset(
"assets/images/sunbird_white.png",
width: 300,
height: 80,
fit: BoxFit.contain,
))),
SizedBox(height: 25),
Container(
width: MediaQuery.of(context).size.width,
child: Center(
child: Text(
"You currently dont have \n any past stays at our Hotels",
textAlign: TextAlign.center,
style:
TextStyle(fontSize: 14, fontFamily: 'Montserrat'),
)))
],
),
),
],
),
),
),
);
}
}
这是错误
A build function returned null.
The offending widget is:
StreamBuilder<dynamic>
Build functions must never return null.
To return an empty space that causes the building widget to fill available room, return
"Container()". To return an empty space that takes as little room as possible, return
"Container(width: 0.0, height: 0.0)".
The relevant error-causing widget was:
StreamBuilder<dynamic>
lib\bookings.dart:65
When the exception was thrown, this was the stack:
............................................... ................................................... ................................................... ........
答案 0 :(得分:0)
未自定义流构建器时出现此问题,因此需要添加在StreamBuilder之后在<>中返回的数据类型
即StreamBuilder 在这里,我告诉我得到的数据是字符串类型 像这样尝试