Home > Net >  Null problem, when I set a value to a variable of class that is placed at another class, in flutter
Null problem, when I set a value to a variable of class that is placed at another class, in flutter

Time:10-07

I created the below class:

class Salon {
  Salon(this._telemetry);
  List<DurationTime> _telemetry= <DurationTime>[];
  List<DurationTime> get telemetry => _telemetry;
  set telemetry(List<DurationTime> value) {
    _telemetry = value;
  }
}
class DurationTime {
  DurationTime(this._p24H,this._p1W,this._p1M,this._p1GD);// to create getter : Alt Insert
  StatisticalParam _p24H;
  StatisticalParam _p1W;
  StatisticalParam _p1M;
  StatisticalParam _p1GD;
  StatisticalParam get p24H => _p24H;
  set p24H(StatisticalParam value) {
    _p24H = value;
  }
  StatisticalParam get p1W => _p1W;
  set p1W(StatisticalParam value) {
    _p1W = value;
  }
  StatisticalParam get p1M => _p1M;
  set p1M(StatisticalParam value) {
    _p1M = value;
  }
  StatisticalParam get p1GD => _p1GD;
  set p1GD(StatisticalParam value) {
    _p1GD = value;
  }
}
class StatisticalParam {
  StatisticalParam(this._min,this._max,this._avg,this._latest);
  TimeValuePair _max;
  TimeValuePair _min;
  TimeValuePair _avg;
  TimeValuePair _latest;
  List<TimeValuePair> _timeSeriesData = <TimeValuePair>[];
  TimeValuePair get max => _max;
  set max(TimeValuePair value) {
    _max = value;
  }
  TimeValuePair get min => _min;
  set min(TimeValuePair value) {
    _min = value;
  }
  TimeValuePair get avr => _avg;
  set avr(TimeValuePair value) {
    _avg = value;
  }
  TimeValuePair get latest => _latest;
  set latest(TimeValuePair value) {
    _latest = value;
  }
  List<TimeValuePair> get timeSeriesData => _timeSeriesData;
  set timeSeriesData(List<TimeValuePair> value) {
    _timeSeriesData = value;
  }
}
class TimeValuePair{
  TimeValuePair(this._value,this._time);
  double _value;
  double _time;
  double get value => _value;
  set value(double value) {
    _value = value;
  }
  double get time => _time;
  set time(double value) {
    _time = value;
  }
}

When I want to create a new instance from salon class, I am forced to define it as below:

Salon? instSalon;

Then I tried to set value to instSalon?.telemetry[0].p24H.max.value,but it didn't set and remained null.

instSalon?.telemetry[0].p24H.max.value=2;

Please let me know, how must assigned value to instSalon?.telemetry[0].p24H.max.value?

CodePudding user response:

First you can define you salon instance like this:

Salon instSalon = Salon([
    DurationTime(
      StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
          TimeValuePair(3, 4), TimeValuePair(8, 9)),
      StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
          TimeValuePair(3, 4), TimeValuePair(8, 9)),
      StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
          TimeValuePair(3, 4), TimeValuePair(8, 9)),
      StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
          TimeValuePair(3, 4), TimeValuePair(8, 9)),
    )
  ]);

Second you can not set variable like that in a list if you want to change some item in that list you should do this, let's assume you want to change first item in your list:

List<DurationTime> _telemetry = [...];
DurationTime newItem = DurationTime(
          StatisticalParam(TimeValuePair(76, 87), TimeValuePair(12, 32),
              TimeValuePair(3, 4), TimeValuePair(8, 9)),
          StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
              TimeValuePair(3, 4), TimeValuePair(8, 9)),
          StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
              TimeValuePair(3, 4), TimeValuePair(8, 9)),
          StatisticalParam(TimeValuePair(12, 10), TimeValuePair(1, 2),
              TimeValuePair(3, 4), TimeValuePair(8, 9)),
        );

_telemetry[0] = newItem;
instSalon.telemetry = _telemetry;

CodePudding user response:

I change my classes as below:

class DurationTime {
  DurationTime(this._p24H,this._p1W,this._p1M,this._p1GD);// to create getter : Alt Insert
  StatisticalParam _p24H;
  StatisticalParam _p1W;
  StatisticalParam _p1M;
  StatisticalParam _p1GD;

  StatisticalParam get p24H => _p24H;

  set p24H(StatisticalParam value) {
    _p24H = value;
  }

  StatisticalParam get p1W => _p1W;

  set p1W(StatisticalParam value) {
    _p1W = value;
  }

  StatisticalParam get p1M => _p1M;

  set p1M(StatisticalParam value) {
    _p1M = value;
  }

  StatisticalParam get p1GD => _p1GD;

  set p1GD(StatisticalParam value) {
    _p1GD = value;
  }
}
class StatisticalParam {
  StatisticalParam(this._min,this._max,this._avg,this._latest,this._timeSeriesData);
  TimeValuePair _max;
  TimeValuePair _min;
  TimeValuePair _avg;
  TimeValuePair _latest;
  // List<TimeValuePair> _timeSeriesData = <TimeValuePair>[];
  List<TimeValuePair> _timeSeriesData = List<TimeValuePair>.filled(5760,TimeValuePair(0,0));


  TimeValuePair get max => _max;

  set max(TimeValuePair value) {
    _max = value;
  }

  TimeValuePair get min => _min;

  set min(TimeValuePair value) {
    _min = value;
  }

  TimeValuePair get avr => _avg;

  set avr(TimeValuePair value) {
    _avg = value;
  }

  TimeValuePair get latest => _latest;

  set latest(TimeValuePair value) {
    _latest = value;
  }

  List<TimeValuePair> get timeSeriesData => _timeSeriesData;

  set timeSeriesData(List<TimeValuePair> value) {
    _timeSeriesData = value;
  }
}
class TimeValuePair{
  TimeValuePair(this._value,this._time);
  double _value;
  double _time;

  double get value => _value;
  set value(double value) {
    _value = value;
  }

  double get time => _time;

  set time(double value) {
    _time = value;
  }
}

Then I create a new instance as follows:

var 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 assign a value to "instDevice.elementAt(0).p24H.latest", value of all instDevice.elementAt(0).p24H.latest to instDevice.elementAt(11).p24H.latest set to new value!! but I want just change instDevice.elementAt(0).p24H.latest. Please let me know how I can set just instDevice.elementAt(0).p24H.latest.

  • Related