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.
SOLVED
The StreamBuilder
works like a loop, too. So the for loop was needless here.
So, the code changes to this:
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);
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);
});
}