Home > Back-end >  Flutter - The for loop continues after the widget returns
Flutter - The for loop continues after the widget returns

Time:12-25

I have 3 documents in the Firestore collection. But I cannot get the other two documents because I perform a return operation once in the for. In the for(final element in data) code, the element needs to get each document in order. I guess because I'm doing a return operation, the for works only once. How can I do that?

Widget getCustomerRepairLog() {
  final user = FirebaseAuth.instance.currentUser!;
  late final _repairLogs =
      FirebaseFirestore.instance.collection('Repair').get();
  return FutureBuilder<QuerySnapshot>(
      future: _repairLogs,
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasData) {
          List<Widget> items = [];
          final data = snapshot.data?.docs;
          if (data == null) return Text("got null");
            for (final eleman in data) {
            debugPrint(eleman.id);
            final currentData = eleman.data() as Map<String, dynamic>;
            if (currentData['email']! == user.email) {
              for (final item in currentData['title']) {
                items.add(Text("${item}"));
              }
              return Card(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                  child: Column(children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text('Yapılan İşlemler'),
                      ],
                    ),
                    
                    Row(children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          for (final index in currentData['title'])
                            Row(
                              children: [
                                Icon(
                                  Icons.done_all_rounded,
                                  color: LightTheme.iconColor,
                                ),
                                Text(index),
                              ],
                            )
                        ],
                      ),
                    ]),
                    SizedBox(
                      height: 50,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Icon(
                          Icons.calendar_month_rounded,
                          color: LightTheme.iconColor,
                        ),
                        getCustomerRepairLogDate()
                      ],
                    ),
                  ]),
                ),
              );
            }
          
          }
          
        }

CodePudding user response:

Yes your diagnosis is correct:

Once the program hits return, the entire function returns.

So how about doing something similar to what you are doing for items. Just after writing this...

List<Widget> items = [];

... do a similar thing to create a list of cards.

Then, instead of returning a card, do this:

for (final item in currentData['title']) {
    items.add(Text("${item}"));
}
cards.add(   
   Card(
       child ... blah blah, what you currently have there
   )
)

Then at the end of the big loop:

        for (final eleman in data) {

            // blah blah

        }

... you would add the actual return statement:

        return cards
  • Related