Home > Mobile >  How to fix NoSuchMethodError: The method '[]' was called on null?
How to fix NoSuchMethodError: The method '[]' was called on null?

Time:12-12

I have a function that filter data from api and set filtered data to new Map:

_addUpAndGetPercentage(List states) {

  Map<String, dynamic> _states = {
    "Stress": [],
    "Fatigue": [],
    "Anxiety": [],
    "Relaxation": [],
    "Involvement": []
  };

  log("states === $states");  // states === [{externalSessionId: 19dd1148-97e3-4e33-8cd6-9d4ab3b8d64c, iaf: 11, iapf: 9, startTime: null, endTime: null, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}}, {externalSessionId: d1d6637a-a445-467d-b658-91e42b080ed6, iaf: 12, iapf: 8, startTime: null, endTime: null, states: {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}]

  for (int i = 0; i < states.length; i  ) {

    var shortcut = states[i]["states"];

    log("Stress ==== ${states[i]['states'].toString()}");

    if (states[i]["Stress"] != null) {
      _states[i]["Stress"].add(states[i]["Stress"]);
    } else {
      log("ERROOROROROOROR ==== ${states[i]['states'].toString()}");
      _states["Stress"].add(0);
    }

    if (shortcut["Anxiety"] != null) {
      _states["Anxiety"].add(shortcut["Anxiety"]);
    } else {
      _states["Anxiety"].add(0);
    }
  }

  log("_states ===== ${_states.toString()}");

  return _states;
}

And I am getting an error when I change states[i]["Stress"] to shortcut["Stress"] => NoSuchMethodError: The method '[]' was called on null.

But log("Stress ==== ${states[i]['states'].toString()}"); returns me Stress ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}

shortcut["Anxiety"] works properly and doesn't return me an error, only shortcut["Stress"] and I don't understand why..

If I remove shortcut["Stress"] and set states[i]["Stress"] then error doesn't come up but it doesn't work like I want to, this is my logs when I set states[i]["Stress"]:

states === [{externalSessionId: 19dd1148-97e3-4e33-8cd6-9d4ab3b8d64c, iaf: 11, iapf: 9, startTime: null, endTime: null, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}}, {externalSessionId: d1d6637a-a445-467d-b658-91e42b080ed6, iaf: 12, iapf: 8, startTime: null, endTime: null, states: {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}]
[log] Stress ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] ERROOROROROOROR ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] Stress ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] ERROOROROROOROR ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}

[log] _states ===== {Stress: [0, 0], Anxiety: [2, 5]}
states === [{externalSessionId: 19dd1148-97e3-4e33-8cd6-9d4ab3b8d64c, iaf: 11, iapf: 9, startTime: null, endTime: null, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}}, {externalSessionId: d1d6637a-a445-467d-b658-91e42b080ed6, iaf: 12, iapf: 8, startTime: null, endTime: null, states: {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}]
[log] Stress ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] ERROOROROROOROR ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] Stress ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] ERROOROROROOROR ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] _states ===== {Stress: [0, 0], Anxiety: [2, 5]}

Stress should be [1,1]

If I add:

if (shortcut["Involvement"] != null) {
      _states["Involvement"].add(shortcut["Involvement"]);
    } else {
      _states["Involvement"].add(0);
    }

Then this involvement works properly, if add relaxation then relaxation returns me null error like stress..

CodePudding user response:

I believe since Stress is part of states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}

Instead of this:

 if (states[i]["Stress"] != null) {
      _states[i]["Stress"].add(states[i]["Stress"]);
    } else {
      log("ERROOROROROOROR ==== ${states[i]['states'].toString()}");
      _states["Stress"].add(0);
    }

you should have this:

 if (states[i]["states"]["Stress"] != null) {
      _states[i]["Stress"].add(states[i]["states"]["Stress"]);
    } else {
      log("ERROOROROROOROR ==== ${states[i]['states'].toString()}");
      _states["Stress"].add(0);
    }
  • Related