So i have made a screen which has a future builder and takes getJobs() as the future. The problem is that the future builder gets null data because the loop of snapshot documents doesnot stop. Here is the Code:
Future<List<Job>> getJobs() async {
var fbuser = await FirebaseAuth.instance.currentUser();
// print('got user');
var currentUser =
await Firestore.instance.collection('user').document(fbuser.uid).get();
setState(() {
seeker = currentUser.data['seeker'];
// print('seeker value = $seeker');
});
var snapshots = await Firestore.instance.collection('jobs').getDocuments();
var snapshotDocuments = snapshots.documents;
List<Job> jobs = [];
// print('got Jobs');
for (var docs in snapshotDocuments) {
var job = Job(
uid: docs.data['uid'],
salaryTo: docs.data['salTo'],
salaryFrom: docs.data['salFrom'],
exp: docs.data['exp'],
loc: docs.data['loc'],
title: docs.data['title'],
desc: docs.data['desc'],
skill: docs.data['skill'],
companyName: docs.data['companyName'],
);
print(job);
jobs.add(job);
}
print('returning $jobs');
return jobs;
}
And here is the build method
Widget build(BuildContext context) {
return Scaffold(
//appBar: AppBar(),
body: FutureBuilder(
future: getJobs(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
// print(snapshot.hasData);
if (snapshot.data == null)
return Center(
child: CircularProgressIndicator(),
);
else
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Center(child: Text('GOT DATA'),);
});
},
),
floatingActionButton: Visibility(
visible: seeker ? false : true,
child: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add_box),
)),
);
}
Thanks for your help !!
EDIT Ok, I have found that error is in this block
var job = Job(
uid: snapshotDocuments[i].data['uid'],
salaryTo: snapshotDocuments[i].data['salTo'],
salaryFrom: snapshotDocuments[i].data['salFrom'],
exp: snapshotDocuments[i].data['exp'],
loc: snapshotDocuments[i].data['loc'],
title: snapshotDocuments[i].data['title'],
desc: snapshotDocuments[i].data['desc'],
skill: snapshotDocuments[i].data['skill'],
companyName: snapshotDocuments[i].data['companyName'],
);
If I remove the job class instance creation the data loop ends, and I dont know why is that happening.
setState()inside thegetJobs()is the issue. It asks flutter to redraw the widget thus calling itself recursively.setState()still gives you the loading circle and nothing else?