0

Consider the following data structure:

{
    "company": {
        "idCompany1": {
            "data": {
                "address": "",
                "companyName": "Company 1",
                "logo": "assets/Logo1.png",
                "nit": "",
                "phone": ""
            }
        },
        "idCompany2": {
            "data": {
                "address": "",
                "companyName": "Company 2",
                "logo": "assets/Logo2.png",
                "nit": "",
                "phone": ""
            }
        },
        "idCompany3": {
            "data": {
                "address": "",
                "companyName": "Company 3",
                "logo": "assets/Logo3.png",
                "nit": "",
                "phone": ""
            }
        }
    },
    "users": {
        "idUser1": {
            "data": "user1@test.com",
            "companies": {
                "idCompany1": true,
                "idCompany3": true
            }
        },
        "idUser2": {
            "data": "user2@test.com",
            "companies": {
                "idCompany2": true
            }
        }
    }
}

Basically what I need to do in the case of user1 is to read the data of the companies to which it belongs, this is Company 1 and Company 3. How can I do that?

The way I found, is by obtaining a list of IDs of those companies, which I have in listaIdEmpresas and then consulting each one through a forEach loop in the following way:

Future<List<EmpresaDatosModel>> cargarEmpresaDatosListado(List<String> listaIdEmpresas) async {

    final List<EmpresaDatosModel> listaEmpresas = new List(); 

    listaIdEmpresas.forEach((id) async {
      Query resp = db.child('company/$id/data');

      final snapshot = await resp.once();

      final temp = EmpresaDatosModel.fromJson(Map<String,dynamic>.from(snapshot.value));
      temp.idEmpresa = id;
      listaEmpresas.add(temp);
      print('${temp.companyName} up');

      await resp.once().then((snapshot) {});
    });
    listaEmpresas.forEach((element) {print('Emp ${element.companyName}');});

    return listaEmpresas; 
}

However, this process is not efficient and I need to manage a delay for waiting the loop.

What would be the right way to do query data from a list of Ids directly?

David L
  • 1,134
  • 7
  • 35
  • "However, this process is not efficient" Firebase loads all data over a single connection, which means there is *very* little performance difference between 10 times loading 1 item vs 1 time loading 10 items. See https://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786. If you experience something different, you may want to edit your question to show how you measured that. – Frank van Puffelen Nov 28 '20 at 15:05
  • @puf, I think it is not efficient because I had to include an aditional delay to get full data. The original problem was explained in https://stackoverflow.com/questions/65015886/how-to-read-from-an-id-list-in-firebase/65017051#65017051 – David L Nov 28 '20 at 18:40

0 Answers0