Reputation: 77
Future fetchData() async{
List<Profile> list=[];
list = await Firestore.instance
.collection("users").getDocuments().then((querySnapshot){
querySnapshot.documents.forEach((document) {
list.add( Profile(
photos: [document['photoUrl'],],
name: document['nickname'],
age: 2,
distance: 2,
education: '3',
bio:"a"
));
});
print(list);
print('list');
return list; //[Instance of 'Profile']
});
}
class _MainControllerState extends State<MainController> {
@override
Widget build(BuildContext context) {
return new Container(
padding: EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 30.0),
child: FutureBuilder(
future: fetchData(),
builder: (
BuildContext context,
AsyncSnapshot snapshot,
){
if (snapshot.connectionState == ConnectionState.done ) {
if (snapshot.data == null) {
print(snapshot);//AsyncSnapshot<dynamic>(ConnectionState.done, null, null)
return Text('no data');
} else {
print('matches');
print(snapshot);
List<Match> matches = snapshot.data
.map((Profile profile) => Match(profile: profile))
.toList();
return CardStack(
matchEngine: MatchEngine(matches: matches),
);
}
} else if (snapshot.error) {
print('matches1');
} else {
print('matches2');
}
return CardStack();
},
),
);
}
}
when I print out the list , I can get [Instance of 'Profile'],but when I print data in snapshot, I can only get AsyncSnapshot(ConnectionState.done, null, null) , how can I fix that error?
I 've tried to change snapshot.connectionState == ConnectionState.done to snapshot.has data. but it dose not work.
Upvotes: 0
Views: 100
Reputation: 77
Future<List<Profile>> fetchData() async{
List<Profile> list=[];
list = await Firestore.instance
.collection("users").getDocuments().then((querySnapshot) async {
querySnapshot.documents.forEach((document) {
list.add( Profile(
photos: [document['photoUrl'],],
name: document['nickname'],
age: 2,
distance: 2,
education: '3',
bio:"a"
));
});
print(list);
print('list');
return list; //[Instance of 'Profile']
});
return list; //[Instance of 'Profile']
}
Upvotes: 0
Reputation: 1067
FutureBuilder
Doesn't know what are you returning from fetchData()
, so you have to specify that you will be returning a List<Profile>
. As Future
without a type is same as Future<void>
.
replace:
Future fetchData() async{...}
with:
Future<List<Profile>> fetchData() async{...}
Similarly sepecify that in FutureBuilder
Instead of:
builder: (
BuildContext context,
AsyncSnapshot snapshot,
){...}
Do this:
builder: (
BuildContext context,
AsyncSnapshot<List<Profile>> snapshot,
){...}
Hope this helps.
Upvotes: 3