2

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

database

The problem is that the StreamBuilder is showing the first 10 values from the database 10 times. I want to show only one time.

5
  • I don't immediately see where the duplication would come from in the code you shared. I recommend setting a breakpoint on if (snapshot.hasData) {, running in the debugger, and stepping through the code line by line to check where the duplicates come from. Commented Nov 29, 2022 at 14:28
  • It's seem that itemInfo.forEach get the key from the snapshot.data and 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 iteration Commented Nov 30, 2022 at 2:53
  • I don't know that either. Keep in mind that we can't see how myRef and 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. Commented Nov 30, 2022 at 4:48
  • The keys in my JSON are the 8-digit codes showed in the screenshot. But don't worry, I solved the problem, the StreamBuilder works like a loop too. So the for loop was needless here. Thank you for your help. Commented Nov 30, 2022 at 14:12
  • myRef.orderByKey().limitToFirst(10).onValue should be assigned to a variable in initState and then the variable used as the stream: parameter. Otherwise, every time the build runs, the database call is rerun. Commented Nov 30, 2022 at 19:31

1 Answer 1

1

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);
                  });
                }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.