gpt4 book ai didi

flutter - 为什么一个功能会在它的时间之前工作?

转载 作者:IT王子 更新时间:2023-10-29 07:11:31 24 4
gpt4 key购买 nike

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

我曾尝试在使用 Timer 之前强制返回保持,但它不允许我在 Timer 上放置一个返回值或在其中放置一个小部件。我还尝试通过建立条件来强制它 if(recepies.length == professionals_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);

}
);
}

最佳答案

您需要使用 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;
}

关于flutter - 为什么一个功能会在它的时间之前工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56295689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com