I'm trying to query for data in Firebase with the following method:
static Future<QuerySnapshot> getUserData(String creatorId) {
Future<QuerySnapshot> data = _firestore
.collection('users')
.where('creatorId', isEqualTo: creatorId)
.getDocuments();
return data;
}
I'm then trying to access the data via this FutureBuilder:
body: FutureBuilder(
future: DatabaseService.getUserData(widget.ride.creatorId),
//future: DatabaseService.getUserData(widget.ride.creatorId),
builder: (context, snapshot) {
// if (!snapshot.hasData) {
// }
//User user = User.fromDoc(snapshot.data);
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Color(0xff192C43),
valueColor: AlwaysStoppedAnimation(
Color(0xff213a59),
),
),
);
}
User user = User.fromDoc(snapshot.data.documents[0]);
return SearchCardItemExtended(user: user, ride: widget.ride,);
},
),
There is always only one User with the same creatorId. That is why I am calling the document on [0].
When I tap on the button that calls the FutureBuilder I get the following Exception:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following RangeError was thrown building FutureBuilder<QuerySnapshot>(dirty, state: _FutureBuilderState<QuerySnapshot>#cff34):
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was:
FutureBuilder<QuerySnapshot> file:///C:/Users/Paul/AndroidStudioProjects/leaf/leaf/lib/screens/search_card_info.dart:61:13
When the exception was thrown, this was the stack:
#0 List.[] (dart:core-patch/growable_array.dart:149:60)
#1 _SearchCardInfoState.build.<anonymous closure> (package:leaf/screens/search_card_info.dart:80:59)
#2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
A very similar query and FutureBuilder elsewhere in the Code work. This is the other FutureBuilder:
body: FutureBuilder(
future: DatabaseService.searchRides(origin, destination, date, time),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Color(0xff192C43),
valueColor: AlwaysStoppedAnimation(
Color(0xff213a59),
),
),
);
}
if (snapshot.data.documents.length == 0) {
return Center(
child: Text(
'Uppss...\n'
'Leider wurden keine passenden Fahrten gefunden.\n'
'Schau doch später noch mal vorbei.',
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'UbuntuLight',
fontSize: 14,
color: Color(0xffE6EFE9),
height: 1.6,
),
),
);
}
return ListView.builder(
physics: new BouncingScrollPhysics(),
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
Ride ride = Ride.fromDoc(snapshot.data.documents[index]);
return SearchCardItem(num: index, ride: ride);
},
);
},
),
What could be the problem here?
Jus change your code like this.
You are facing this error because there is no result from firebase then your are trying to call
|0]but there is no element at 0. You have to wrap it in a conditinnal way. So it will be executed only when there are more then on user insnapshot.data