0

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.

6
  • i suppose the setState() inside the getJobs() is the issue. It asks flutter to redraw the widget thus calling itself recursively. Commented Jul 17, 2020 at 4:04
  • No still same issue Commented Jul 17, 2020 at 4:20
  • so removing the setState() still gives you the loading circle and nothing else? Commented Jul 17, 2020 at 4:36
  • I have updated the question, kindly give it a look and tell me Commented Jul 17, 2020 at 5:05
  • 1
    Thanks, that solved the problem. I was assigning a string to int. Commented Jul 17, 2020 at 5:19

1 Answer 1

0

FutureBuilder will keep returing snapshot.data as null if there is an exception due to any reason e.g. server issues, internet not available or there is some exception generating from your own code.

So, it's always advisable to have 3 cases in the FutureBuilder.

  1. Valid data arrived
  2. Data is loading
  3. Error during data load

Check this SO post

Sign up to request clarification or add additional context in comments.

1 Comment

The problem was in the Job section. snapshot was returning a string and I was storing it in an int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.