为什么功能在其时间之前起作用?

时间:2019-05-24 15:34:45

标签: flutter dart

我有这段代码,除了一件事,它工作正常。显然返回_buildList(context);是在Queries()。getProfessionals(medic_id).then((QuerySnapshot medics)之前发生的,但是正如您在代码中所看到的,我要求它在之后发生。

我曾试图通过在使用计时器之前将返回值保持为强制进行尝试,但是它不允许我将返回值放在计时器上或放入小部件中。 我还尝试通过建立条件if(recepies.length == professional_name.lengh)return _buildList(context)来强制执行此操作;但它给我错误“ StreamBuilder绝不能返回null”。我不知道该怎么办。

Widget _searchRecepies(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance.collection('recepie').where('patient_id', isEqualTo: userId).snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return LinearProgressIndicator();

      recepies_id = snapshot.data.documents.map((document){
        return document.documentID;
      }).toList();

      recepies = snapshot.data.documents.map((document){
        return document.data;
        //print(document.documentID);
      }).toList();

      for (Map recep in recepies) {
        recepies_dateadded.add(recep['date_added']);
        recepies_diagnosis.add(recep['diagnosis']);
        medic_id = recep['medic_id'];
        Queries().getProfessionals(medic_id).then((QuerySnapshot medics){
          print(medics.documents[0].data);
          _med = medics.documents[0].data;
          _med_name = _med['names'];
          _med_lastnames = _med['last_names'];
          _med_specility = _med['speciality'];
          _med_rut = _med['rut'];
          professionals_name.add(_med_name);
          professionals_lastnames.add(_med_lastnames);
          professionals_speciality.add(_med_specility);
          professionals_rut.add(_med_rut);
        });
      }
      return _buildList(context);

  }
);
}

1 个答案:

答案 0 :(得分:2)

完成异步过程后,您需要使用FutureBuilder / StreamBuilder /或仅使用StatefulWidget刷新数据,如下所示:

     Widget _searchRecepies(BuildContext context) {
        return StreamBuilder<QuerySnapshot>(
            stream: Firestore.instance
                .collection('recepie')
                .where('patient_id', isEqualTo: userId)
                .snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return LinearProgressIndicator();

              recepies_id = snapshot.data.documents.map((document) {
                return document.documentID;
              }).toList();

              recepies = snapshot.data.documents.map((document) {
                return document.data;
                //print(document.documentID);
              }).toList();

              return FutureBuilder(
                future: _loadAsyncData(recepies),
                builder: (_, snapshot){
                  if (!snapshot.hasData) return LinearProgressIndicator();
                  return _buildList(context);
                },
              );
            });
      }

      _loadAsyncData(List data) async {
        for (Map recep in recepies) {
                recepies_dateadded.add(recep['date_added']);
                recepies_diagnosis.add(recep['diagnosis']);
                medic_id = recep['medic_id'];
               QuerySnapshot medics =  await Queries().getProfessionals(medic_id);
                 print(medics.documents[0].data);
                  _med = medics.documents[0].data;
                  _med_name = _med['names'];
                  _med_lastnames = _med['last_names'];
                  _med_specility = _med['speciality'];
                  _med_rut = _med['rut'];
                  professionals_name.add(_med_name);
                  professionals_lastnames.add(_med_lastnames);
                  professionals_speciality.add(_med_specility);
                  professionals_rut.add(_med_rut);
              }
           return true;   
      }