AnswerBun.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!

Related Questions

Call parent class method (with this context) in child class

2  Asked on December 27, 2021 by rjoxford

 

Replace .(dot) inside number Java between two elements

2  Asked on December 27, 2021 by alex-danilov

 

Filtering an array of objects Vue

2  Asked on December 27, 2021 by graham-morby

     

Is there any good idea to ceil or floor datetime in python3?

2  Asked on December 27, 2021 by asknature

   

How do I use django’s Q with django taggit?

4  Asked on December 27, 2021 by shino

     

When to use UserPassesTestMixin vs PermissionRequiredMixin

2  Asked on December 27, 2021 by david-jay-brady

 

nuxt: pass data from server plugin to client

1  Asked on December 27, 2021

 

Am I Using JDBC Connection Pooling?

4  Asked on December 27, 2021 by ryandlf

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP