如何制作嵌入式for循环?

问题描述 投票:0回答:1

我已经完成了一个嵌入式for循环,该循环正在从Cloud Firestore获取,但是所有项目都被复制到行中。在firestore中,请按照不同的阵列进行修复?

如果可以通过另一种方式在同一屏幕的每一侧上以不同的方式获得它,则为该设置?

谢谢。

下面附有图像的外观

Image for loop

import 'package:flutter/material.dart';

class TimeInfo extends StatelessWidget {
  TimeInfo({
    @required this.routenum,
    this.stop,
    this.timeto,
    this.timefrom,
    this.abv_from,
    this.abv_to,
  });
  final routenum;
  final stop;
  final timeto;
  final timefrom;
  final abv_from;
  final abv_to;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DefaultTabController(
          length: 3,
          child: Scaffold(
            appBar: PreferredSize(
              preferredSize: Size.fromHeight(55.0),
              child: AppBar(
                backgroundColor: Colors.white,
                elevation: 0,
                bottom: TabBar(
                  indicatorWeight: 0,
                    unselectedLabelColor: Colors.red,
                    indicatorSize: TabBarIndicatorSize.label,
                    indicator: BoxDecoration(
                      borderRadius: BorderRadius.circular(50),
                      color: Colors.redAccent,
                    ),
                    tabs: [
                      Tab(
                        child: Container(
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(50),
                              border: Border.all(
                                  color: Colors.redAccent, width: 1)),
                          child: Align(
                            alignment: Alignment.center,
                            child: Text("Weekdays"), 
                          ),
                        ),
                      ),
                    ]),
              ),
            ),
            body: TabBarView(children: [
              SingleChildScrollView(
                  padding: EdgeInsets.all(8.0),
                  child: Center(
                    child: Column(
                      children: <Widget>[
                        DataTable(
                          columns: [
                            DataColumn(
                              label: Text(abv_to + " - " + abv_from),
                            ),
                            DataColumn(
                              label: Text(abv_from + " - " + abv_to),
                            ),
                          ],
                          rows: [
                            for(int i =0; i< timeto.length; i ++)
                              for (int j = 0; j< timefrom.length; j ++)
                            DataRow(
                              cells: [
                                DataCell(Text(timeto[i])),
                                DataCell(Text(timefrom[j])),
                              ]
                            ),
                          ],
                          sortColumnIndex: 0,
                          sortAscending: true,
                        ),
                      ]
                    ),
                  ),
                ),
              Icon(Icons.beach_access),
              Icon(Icons.cloud_download),
            ]),
          )),
    );
  }
}
firebase flutter
1个回答
0
投票

实际上,正在发生的事情是,数据行始终从您创建的循环中获取最后一个值。如果timeto的长度是6,timefrom的长度是7,则它总是填充此。]

DataRow(
                              cells: [
                                DataCell(Text(timeto[5])),
                                DataCell(Text(timefrom[6])),
                              ]
                            ),

因此使用以下代码。它应该为您工作。希望这会有所帮助。

rows: getItems();
  getItems() {
    final dataRows=[];
    for (int i = 0; i < timeto.length; i ++)
      for (int j = 0; j < timefrom.length; j ++)
        dataRows.add(DataRow(
            cells: [
              DataCell(Text(timeto[i])),
              DataCell(Text(timefrom[j])),
            ]
        ));
  }
© www.soinside.com 2019 - 2024. All rights reserved.