问题
My if condition is successfully working and it is retrieving data. But when data is null, I want to display a text in Center(). Text that no offers are added yet. But it isn't displaying anything when data is null. I tried everything. But execution never goes to else and else if conditions. Can someone shed some light that what am I missing?
child: StreamBuilder(
stream: Firestore.instance.collection('BidList').where('PostID',isEqualTo:args.PostId).snapshots(),
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
return Container(
child: Column(
children: <Widget>[
Container(
// margin: EdgeInsets.only(top: 30),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
Container(
//color: Colors.grey[500],
color: Colors.teal[700],
width: 300,
height: 25,
child: Center(
child: Text(
'Offers List',
style: TextStyle(
fontSize: 18,
fontWeight:
FontWeight.bold,
color: Colors.white),
),
),
)
],
),
),
ListTile(
title: Text(
snapshot.data.documents[index].data['Name']
.toString()
.toUpperCase(),
style: TextStyle(fontSize: 14, color: Colors
.black, fontWeight: FontWeight.bold),
),
leading: CircleAvatar(
//backgroundImage: NetworkImage(w),
// backgroundColor: Colors.blueGrey,
backgroundImage: CachedNetworkImageProvider(data
.toString()),
),
// subtitle: Text(username),
//subtitle: Text(accountCreated.toString()),
subtitle: Row(
children: [
Text('Number: ', style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 10),),
Text(
snapshot.data.documents[index]
.data['Number']
.toString()
.toUpperCase(),
style: TextStyle(
fontSize: 12, color: Colors.black45),
),
],
),
//trailing: Text(timeago.format(timestamp.toDate())),
trailing: Container(
margin: EdgeInsets.all(10),
child: Column(
children: [
Text('Bid', style: TextStyle(
fontWeight: FontWeight.bold),),
Text(
snapshot.data.documents[index].data['Bid']
.toString()
.toUpperCase(),
style: TextStyle(fontSize: 12),),
],
),
),
),
Divider(),
],
),
);
}
);
}
else if(snapshot.hasError || snapshot == null) {
return Container(
child: Column(
children: <Widget>[
Container(
// margin: EdgeInsets.only(top: 30),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
Container(
//color: Colors.grey[500],
color: Colors.teal[700],
width: 300,
height: 25,
child: Center(
child: Text(
'Offers List',
style: TextStyle(
fontSize: 18,
fontWeight:
FontWeight.bold,
color: Colors.white),
),
),
)
],
),
),
Center(child: Text('No Reviews Yet'),),
Divider(),
],
),
);
//
}
else{
return Container(
child: Column(
children: <Widget>[
Container(
// margin: EdgeInsets.only(top: 30),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: <Widget>[
Container(
//color: Colors.grey[500],
color: Colors.teal[700],
width: 300,
height: 25,
child: Center(
child: Text(
'Offers List',
style: TextStyle(
fontSize: 18,
fontWeight:
FontWeight.bold,
color: Colors.white),
),
),
)
],
),
),
Center(child: Text('No Reviews Yet'),),
Divider(),
],
),
);
}
},
),
回答1:
If your collection don't have documents or data your can add this condition.
if(snapshot.hasData){
if (snapshot.data.docs.length != 0) {
return ListView.builder();
} else {
return Text("EMPTY DOCUMENTS");
}
}
回答2:
Try using snapshot.data == null instead of snapshot == null
if(snapshot.hasError || snapshot.data == null)
来源:https://stackoverflow.com/questions/64457942/else-condition-not-working-in-flutter-using-cloud-firestore