Home > Mobile >  How to set the value of an object inside another list object in the flutter
How to set the value of an object inside another list object in the flutter

Time:10-27

In my app, I need to get some data and storage them and finally show them in graph. So, I have defined some class as follows: `

class DurationTime {
  StatisticalParam p24H;
  StatisticalParam p1W;
  StatisticalParam p1M;
  StatisticalParam p1GD;
  DurationTime(this.p24H,this.p1W,this.p1M,this.p1GD);// to create getter : Alt Insert
  factory DurationTime.fromMap(Map data) =>  DurationTime(data['p24H'],data['p1W'],data['p1M'],data['p1G']);
}
class StatisticalParam {
  TimeValuePair max;
  TimeValuePair min;
  TimeValuePair avg;
  TimeValuePair latest;
  List<TimeValuePair> timeSeriesData = List<TimeValuePair>.filled(5760,TimeValuePair(0,0));//<TimeValuePair>[];
  StatisticalParam(this.min,this.max,this.avg,this.latest,this.timeSeriesData);

  factory StatisticalParam.fromMap(Map data) => StatisticalParam(data['max'],data['min'],data['avg'],data['latest'],data['timeSeriesData']);
}
class TimeValuePair{
  TimeValuePair(this.value,this.time);
  double value;
  double time;

  factory TimeValuePair.fromMap(Map data) => TimeValuePair(data['value'],data['time']);
}

Then I defined a list with name of instDevice, and initialize it as follow:

List<DurationTime> instDevice = List<DurationTime>.filled(11,
    DurationTime(StatisticalParam(TimeValuePair(0,0), TimeValuePair(0,0), TimeValuePair(0,0), TimeValuePair(0,0),List<TimeValuePair>.filled(5760,TimeValuePair(0,0))),
        StatisticalParam(TimeValuePair(0,0), TimeValuePair(0,0), TimeValuePair(0,0), TimeValuePair(0,0),List<TimeValuePair>.filled(5760,TimeValuePair(0,0))),
        StatisticalParam(TimeValuePair(0,0), TimeValuePair(0,0), TimeValuePair(0,0),TimeValuePair(0,0),List<TimeValuePair>.filled(5760,TimeValuePair(0,0))),
        StatisticalParam(TimeValuePair(0,0), TimeValuePair(0,0), TimeValuePair(0,0),TimeValuePair(0,0),List<TimeValuePair>.filled(5760,TimeValuePair(0,0)))));

When I want to assigne value to

instDevice.elementAt(Index).p24H.latest.value

for Index=1, it is changing for all Index=0 to 11 not only Index=1!! Please let me know, how I can setinstDevice.elementAt(Index).p24H.latest.valuefor specialIndex`?

Please let me know, how I can set instDevice.elementAt(Index).p24H.latest.value for special Index?

CodePudding user response:

Any reason this doesn't work? I might be misunderstanding the issue:

instDevice[index].p24H = newValue;

CodePudding user response:

List.filled will use the same instance for every position. You need a new instance in each position otherwise, yes, you'll update the shared instance in every position. Instead of using List.filled, use List.generate

  List<DurationTime> instDevice = List<DurationTime>.generate(
    11,
    (i) => DurationTime(
      StatisticalParam(
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        List<TimeValuePair>.filled(
          1,
          TimeValuePair(0, 0),
        ),
      ),
      StatisticalParam(
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        List<TimeValuePair>.filled(
          1,
          TimeValuePair(0, 0),
        ),
      ),
      StatisticalParam(
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        List<TimeValuePair>.filled(
          1,
          TimeValuePair(0, 0),
        ),
      ),
      StatisticalParam(
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        TimeValuePair(0, 0),
        List<TimeValuePair>.filled(
          1,
          TimeValuePair(0, 0),
        ),
      ),
    ),
  );
  • Related