I have a problem with showing a database in my app. Here's the code.
SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
StreamBuilder(
stream: myRef.orderByKey().limitToFirst(10).onValue,
builder: ((context, snapshot) {
if (snapshot.hasData) {
final itemInfo = Map<String, dynamic>.from(
(snapshot.data!).snapshot.value as Map);
itemInfo.forEach((key, value) {
final nextItem = Map<String, dynamic>.from(value);
for (int i = 0; i < itemInfo.length; i++) {
List<dynamic> item = List.empty(growable: true);
item.add(key.toString());
item.add(nextItem['Descripcion']);
item.add(nextItem['Cant']);
item.add(nextItem['Ubicacion']);
item.add(nextItem['Um']);
item.add(nextItem['X']);
itemsList.add(item);
}
});
}
return Text('${itemsList.toString()}\n');
})),
],
),
),
),
),
Here's the app screen

The problem is that the StreamBuilder is showing the first 10 values from the database 10 times. I want to show only one time.
if (snapshot.hasData) {, running in the debugger, and stepping through the code line by line to check where the duplicates come from.itemInfo.forEachget the key from thesnapshot.dataand enters in the for loop and do the iterations 10 times, then changes to the next key and do it the same. I don't understand why the key doesn't change with each iterationmyRefand what the JSON in the database is that it points to. The output in the screenshot may make sense to you, but it's pretty unreadable to me.StreamBuilderworks like a loop too. So the for loop was needless here. Thank you for your help.