Flutter Firestore 快照文档循环不会停止

Flutter Firestore Snapshot Documents loop doesnot stop

所以我做了一个屏幕,它有一个 future builder 并将 getJobs() 作为 future。问题是未来的构建器会得到空数据,因为快照文档的循环不会停止。这是代码:

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;
  }

这是构建方法

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),
          )),
    );
  }

感谢您的帮助!!

编辑 好的,我发现错误在这个块

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'],
      );

如果我删除作业 class 实例创建,数据循环结束,我不知道为什么会这样。

如果由于任何原因出现异常,

FutureBuilder 将继续返回 snapshot.data 作为 null服务器问题、互联网不可用或您自己的代码生成了一些异常。

因此,在 FutureBuilder 中设置 3 个案例总是明智的。

  1. 有效数据到达
  2. 数据正在加载
  3. 数据加载时出错

勾选