0

I’m reading my SQlite database, my funcion getArmarCartillaPorDescripcion2() works good. In order to add temporalVar(type: List ) to finalVar (type: List ), i mean many List inside a List, i use forEach method. However, i can see temporalVar have data all the time but “add” method doesn’t work, at the end finalVar returns [] (no data). I tried changing type of temporal var to List<List>,List<List>,List, etc, but the same problem.

Code forEach where is the problem finalVar.add(temporalvar)

listaUniqueGrupoFito.forEach((element) async{
  final valor= await ArmarCartillaDbProvider.db.getArmarCartillaPorDescripcion2('001',element);
  valor!.forEach((element) {
    temporalvar.add(element.descripcion);
  });
  finalVar.add(temporalvar);
  print("temporalvar: $temporalvar");
  temporalvar.clear();
});
print("finalVar: $finalVar");

The funcion i read and its return (works good):

Future<List<ArmarCartillaModel>?> getArmarCartillaPorDescripcion2(String codCartilla, String grupo) async{
    final db= await database;
    final res = await db.rawQuery('''
     SELECT  * FROM ArmarCartilla WHERE COD_CARTILLA= '$codCartilla' and GRUPO='$grupo' 
    ''');
    return res.isNotEmpty
          ? res.map((s) => ArmarCartillaModel.fromJson(s)).toList()
          :[];
  }

Flutter debug console answer, every loop has data. However, finalVar: []. I want to assing each each temporalvar loop to my principal List.

I/flutter (12822): finalVar: []
I/flutter (12822): temporalvar loop: [Nº Brotes infestados, Nº Ninfas/Brote, Nº Adultos/Brote, Nº Infloresc. Infestadas]
I/flutter (12822): temporalvar loop: [Nº Brotes infestados, Nº Larvas/Brote, Nº de Hojas Infestadas, N° Larvas chicas, Nº Infloresc. Infestadas]
I/flutter (12822): temporalvar loop: [Nº Brotes infestados, Nº de masas/Brote, N° Larvas chicas, N° Larvas grandes, Nº de Hojas Infestadas, Nº de masas/hoja, N° Larvas chicas, N° Larvas grandes]
  • You aren't making a copy of `temporalvar` when you add it to `finalVar`, and then you promptly clear `temporalvar`. – jamesdlin Sep 10 '21 at 02:37
  • A separate problem is that you're using [`Iterable.forEach` with an asynchronous callback, which is always wrong](https://stackoverflow.com/a/63719805/). – jamesdlin Sep 10 '21 at 02:40

0 Answers0