如何从Firebase中的ID列表中读取? [英] How to read from an ID list in Firebase?
问题描述
我有一个ID列表(
I have a list of IDs (Future<List<String>> _listaIdEmpresasF
) and I would like to query data based on each ID in the list. I'm using two FutureBuilders: The first one to get the list of IDs, and the second one to display the data related of each ID.
我的代码看起来像这样...
My code looks like this...
@override
void initState() {
super.initState();
_listaIdEmpresasF = _usuarioBloc.cargarEmpresasDeUsuario(_prefs.idUsuario);
}
Widget _crearListado(BuildContext context) {
return FutureBuilder(
future: _listaIdEmpresasF,
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.connectionState);
if (snapshot.connectionState == ConnectionState.done && snapshot.hasData != null){
final List<String> _listaIdEmpresas = snapshot.data;
return FutureBuilder(
future: _empresaDatosBloc.cargarEmpresaDatosListado(_listaIdEmpresas),
builder: (BuildContext context, AsyncSnapshot snapshot2) {
print(snapshot2.connectionState);
if (snapshot2.connectionState == ConnectionState.done && snapshot2.hasdata) {
final List<EmpresaDatosModel> _listaEmpresas = snapshot2.data;
return Stack(
children: <Widget>[
ListView.builder(
itemCount: _listaEmpresas.length,
itemBuilder: (context, i) {
return _crearItem(context, _listaEmpresas[i], i);
},
padding: EdgeInsets.only(left: 10.0, right: 10.0, top: 0.0, bottom: 20.0),
),
],
);
} else return Align(
alignment: Alignment.center,
child: Center (child: Image(image: AssetImage('Preloader.gif'), height: 500.0,)),
);
},
);
}
else return Align(
alignment: Alignment.center,
child: Center (child: Image(image: AssetImage('Preloader.gif'), height: 500.0,)),
);
},
);
}
要从每个ID获取数据,我正在使用如下循环:
To get the data from each ID, I'm using a loop as follows:
Future<List<EmpresaDatosModel>> cargarEmpresaDatosListado(List<String> listaIdEmpresas) async {
final List<EmpresaDatosModel> listaEmpresas = new List();
listaIdEmpresas.forEach((id) async {
Query resp = db.child('empresas/$id/datos');
final snapshot = await resp.once();
if (snapshot.value == null) return [];
final temp = EmpresaDatosModel.fromJson(Map<String,dynamic>.from(snapshot.value));
temp.idEmpresa = id;
listaEmpresas.add(temp);
print('${temp.nombre} subida');
await resp.once().then((snapshot) {
print("Los Datos de una Empresa se cargaron totalmente - ${temp.nombre}");
});
});
listaEmpresas.forEach((element) {print('Emp ${element.nombre}');});
return listaEmpresas;
}
问题:我无法获取完整数据.例如,如果我在_listaIdEmpresas中具有三个ID,则该应用有时会显示一个公司(与第一个ID相关)的列表,有时会显示两个公司(第一个和第二个ID),有时还会显示三个公司的列表.我认为这是因为第二个快照的ConnectionState在读取所有三个循环之前完成的.
The problem: I can't be able to obtain the full data. For example, if I have three IDs in _listaIdEmpresas, sometimes the app displays a list of one company (related to the first ID), sometimes two companies (first and secod ID) and sometimes the three companies. I supposed this is because the ConnectionState of the second snapshot is done before reading all three loops..
我试图添加一个条件,但是没有用...
I tried to add a condition but it didn't work...
if (snapshot2.connectionState == ConnectionState.done && snapshot2.data.length == _listaIdEmpresas.length)
由于我配置的查询安全规则,我无法查询所有公司并将其过滤为公司列表.
I cannot query all companies and filter it as a list of companies due to query security rules that I configured.
我想在查询参数中包含ID的特定列表,但我不知道是否可能.
I would like to include the specific list of IDs in the query parameters, but I don't know if it's possible.
如何正确地从ID列表中读取数据?
How is the right way to read data from a list of IDs?
推荐答案
snapshot.key将为您提供该商品的ID
snapshot.key will give you the id of the item
这篇关于如何从Firebase中的ID列表中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!