TransWikia.com

Cant loop through List and display in DataTable

Stack Overflow Asked by Finchy70 on December 25, 2020

I have a flutter screen that displays Battery data in a DataTable.

I have a provider that returns a List of Battery objects and I have tried to loop through the list with the following.

FutureBuilder(
                future: Provider.of<Batteries>(context)
                    .fetchBatteriesByInspectionId(widget.inspectionClient.id),
                builder: (ctx, dataSnapshot) {
                  return SingleChildScrollView(
                    child: DataTable(
                      columns: const <DataColumn>[
                        DataColumn(
                          label: Text(
                            "ID",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Trip V",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Output V",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Battery V",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Manufacturer",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Install Date",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Temperature",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            "Option",
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                      ],
                      rows: Provider.of<Batteries>(context)
                          .batteries
                          .map(
                            (bat) => DataRow(
                              cells: [
                                DataCell(Text(bat.name)),
                                DataCell(Text(bat.trippingVoltage)),
                                DataCell(Text(bat.outputVoltage)),
                                DataCell(Text(bat.batteryVoltage)),
                                DataCell(Text(bat.batteryMake)),
                                DataCell(Text(bat.installDate)),
                                DataCell(Text(bat.temperature)),
                                DataCell(
                                  RaisedButton(
                                    onPressed: () {},
                                    elevation: 5,
                                    color: Colors.amber,
                                    child: Text("Edit"),
                                  ),
                                ),
                              ],
                            ),
                          )
                          .toList(),
                    ),
                  );
                },
              )

It does not work. It returns no errors, just displays an empty DataTable with just the header row. I have checked the Provider.of(context).batteries and it contains 1 Battery object.

Any ideas?

Update 1 Add Provider code

import 'package:flutter/material.dart';
import '../models/battery.dart';
import '../helpers/battery_db_helper.dart';

class Batteries with ChangeNotifier {
  List<Battery> _batteries = [];
  Battery _selectedBattery;

  Battery get selectedBattery {
    return _selectedBattery;
  }

  List<Battery> get batteries {
    return _batteries;
  }

  Future<void> fetchSelectedBattery(batteryId) async {
    BatteryDBHelper batteryDBHelper = BatteryDBHelper();
    Battery battery = await batteryDBHelper.getBatteryById(batteryId);
    _selectedBattery = battery;
    notifyListeners();
  }

  Future<void> fetchBatteriesByInspectionId(inspectionId) async {
    BatteryDBHelper batteryDBHelper = BatteryDBHelper();
    _batteries = [];
    _batteries = await batteryDBHelper.getBatteriesByInspectionId(inspectionId);
    print("Getting ${_batteries.length} battery / batteries!");
    notifyListeners();
  }
}

One Answer

try add condition of dataSnapshot like

builder: (ctx, dataSnapshot) {
  if(dataSnapshot.hasData)
    return SingleChildScrollView(
      child: DataTable(
        columns: const <DataColumn>[
          DataColumn(
            label: Text(
              "ID",
              style: TextStyle(fontStyle: FontStyle.italic),
            ),
          ),
          DataColumn(
            label: Text(
  ...
  
  else return Container();

Answered by hyobbb on December 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP