title: Flutter how to change value of a class object
body: <p>I'm new to Flutter. I try to work with class but I don't see how to apply a new value to a parameter of my User object in a foreach loop because the parameter is a variable.</p>
  <p><strong>My class User :</strong></p>
  <pre><code>class UsersList{
    List&lt;User&gt; users;
  
    UsersList({
      this.users
    });
  
    factory UsersList.fromJson(List&lt;dynamic&gt; json) {
      List&lt;User&gt; users= List&lt;User&gt;();
  
      users= json.map((i) =&gt; User.fromJson(i)).toList();
  
      return UsersList(
        users : users
      );
    }
  
    Map&lt;String, dynamic&gt; toJson() =&gt; {
      &quot;users&quot;: users,
    };
  
    @override
    String toString() {
      return '{'
          'users: ${this.users},'
          '}';
    }
  }
  
  class User{
    int type;
    String name;
    bool gender;
    num distance;
    num index;
    num position;
    num fuel;
    num values
  
    User({
      this.type,
      this.name,
      this.gender,
    });
  
    factory User.fromJson(Map&lt;String, dynamic&gt; json){
      return User(
        type : json['type'],
        name: json['name'],
        gender : json['gender'],
        distance: json['distance'],
        index: json['index'],
        position: json['position'],
        fuel: json['fuel'],
        values: json['values'],
      );
    }
  
    Map&lt;String, dynamic&gt; toJson() =&gt; {
      &quot;type&quot; : type,
      &quot;name&quot; : name,
      &quot;gender&quot; : gender,
      &quot;distance&quot; : distance,
      &quot;index&quot; : index,
      &quot;position&quot; : position,
      &quot;fuel&quot; : fuel,
      &quot;values&quot; : values,
    };
  
    @override
    String toString() {
      return '{'
          'type : ${this.type},'
          'name: ${this.name},'
          'gender : ${this.gender},'
          'distance: ${this.distance},'
          'index: ${this.index},'
          'position: ${this.position},'
          'fuel: ${this.fuel},'
          'values: ${this.values},'
          '}';
    }
   }
  </code></pre>
  <p><strong>And this is my function :</strong></p>
  <pre><code>static User rebuildUser(User item, UsersList usersJson, FormCalculator formData)
    {
      if(usersJson== null || usersJson.users.isEmpty) return null;
  
      final User _rebuild = User();
      List&lt;User&gt; _allUsersType = usersJson.users.where((c) =&gt; (c.type == item.type)).toList();
      User_upperItem = _allUsersType.firstWhere((c) =&gt; (c.distance &gt; item.distance));
  
      item.toJson().forEach((key, value) {
        if (key != &quot;type&quot; &amp;&amp; key != 'distance' &amp;&amp; key != 'index')
        {
          final _newValue = CalculatorInitial.calculateNew(item, _upperItem, formData.distance, &quot;distance&quot;, key);
          _rebuild.toJson()[key] = _newValue; // ----- This not work ! -----
        }
      });
  
      return _rebuild; // The values ​​of my User rebuild are unchanged because all of which are null
  
    }
  </code></pre>
  <p>This function allows me to reconstruct a user with data calculated between 2 users.
  This function works because I get the desired result. I don't see <strong>how to apply these new values ​​in my user object</strong>. I use the &quot;toJson ()&quot; function to be able to have the key as a variable but it doesn't seem to apply the new values ​​to my object.</p>