Home > front end >  Invalid value: Valid value range is empty: 0 with for loop
Invalid value: Valid value range is empty: 0 with for loop

Time:10-27

void _set() {
  late List<int> _babus = [];
  for (int i = 0; i < 31; i  ) {
    _babus.add(_consumptionData['Energies'][0]['EnergyInfo']['Last30DayData']
            .values
            .toList()[i] ??
        0);
  }
  print(_babus);
}

I need to assign 0 if the value is empty but I'am gettin Invalid value: Valid value range is empty: 0

void _set() {
  late List<int> _babus = [];
  for (int i = 0; i < 31; i  ) {
    if (_consumptionData['Energies'][0]['EnergyInfo']['Last30DayData'] !=
        null) {
      _babus.add(_consumptionData['Energies'][0]['EnergyInfo']
              ['Last30DayData']
          .values
          .toList()[i]);
    } else {
      (_babus.add(0));
    }
  }
  print(_babus);
}

CodePudding user response:

 void _set() {
    late List<int> _babus = [];

    for (int i = 0; i < 31; i  ) {
      if (_consumptionData['Energies'][0]['EnergyInfo']['Last30DayData']
              .values
              .toList()
              .isEmpty ==
          true) {
        
        _babus.add(0);
      } else {
        
        _babus.add(_consumptionData['Energies'][0]['EnergyInfo']
            ['Last30DayData'].values.toList()[i]);
      }
    }

    print(_babus);
  }

CodePudding user response:

I know you have found an answer using an imperative approach, but I thought it might be fun to show a declarative alternative. I am unsure exactly how your code works, so I made an assumption or two. however, I have adapted it a bit to have a list that stores 31 days worth of values (I just assumed it is for a day of a 31-day month). This way, it won't matter if the value is 0 because it will be replaced by the value in the list. Please note that we need more details about _consumptionData to transfer details from it to the new map.

void main() {
  print(set());
}

set() {
  return {'Energies' :  
    values
        .map((e) => {
              'EnergyInfo': {'Last30DayData': e}
            })
        .toList()
  };
}

List<double> values = [
  101.0,
  102.0,
  103.0,
  104.0,
  105.0,
  106.0,
  107.0,
  108.0,
  109.0,
  110.0,
  111.0,
  112.0,
  113.0,
  114.0,
  115.0,
  116.0,
  117.0,
  118.0,
  119.0,
  120.0,
  121.0,
  122.0,
  123.0,
  124.0,
  125.0,
  126.0,
  127.0,
  128.0,
  129.0,
  130.0,
  131.0,
];

output

flutter: {Energies: [{EnergyInfo: {Last30DayData: 101.0}}, {EnergyInfo: {Last30DayData: 102.0}}, {EnergyInfo: {Last30DayData: 103.0}}, {EnergyInfo: {Last30DayData: 104.0}}, {EnergyInfo: {Last30DayData: 105.0}}, {EnergyInfo: {Last30DayData: 106.0}}, {EnergyInfo: {Last30DayData: 107.0}}, {EnergyInfo: {Last30DayData: 108.0}}, {EnergyInfo: {Last30DayData: 109.0}}, {EnergyInfo: {Last30DayData: 110.0}}, {EnergyInfo: {Last30DayData: 111.0}}, {EnergyInfo: {Last30DayData: 112.0}}, {EnergyInfo: {Last30DayData: 113.0}}, {EnergyInfo: {Last30DayData: 114.0}}, {EnergyInfo: {Last30DayData: 115.0}}, {EnergyInfo: {Last30DayData: 116.0}}, {EnergyInfo: {Last30DayData: 117.0}}, {EnergyInfo: {Last30DayData: 118.0}}, {EnergyInfo: {Last30DayData: 119.0}}, {EnergyInfo: {Last30DayData: 120.0}}, {EnergyInfo: {Last30DayData: 121.0}}, {EnergyInfo: {Last30DayData: 122.0}}, {EnergyInfo: {Last30DayData: 123.0}}, {EnergyInfo: {Last30DayData: 124.0}}, {EnergyInfo: {Last30DayData: 125.0}}, {EnergyInfo: {Last30DayData: 126.0}}, <…>
  • Related