title: Flutter Use foreach in a class
body: <p>I'm new to Flutter and I tried to work with class but I have some problems.
  I have a class UsersList and User.
  When I work whit UsersList who is a List of User, how to use foreach method ?
  The length, last, remove Last, add, ... attributes are not available either. How to do this?</p>
  <p><strong>My class UsersList and 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;
  
    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'],
      );
    }
  
    Map&lt;String, dynamic&gt; toJson() =&gt; {
      &quot;type&quot; : type,
      &quot;name&quot; : name,
      &quot;gender&quot; : gender,
    };
  
    @override
    String toString() {
      return '{'
          'type : ${this.type},'
          'name: ${this.name},'
          'gender : ${this.gender},'
          '}';
    }
   }
  
  </code></pre>
  <p><strong>And this is my function :</strong>
  I use in this the UserList class. This code works when I don't use class...</p>
  <pre><code>static UsersList getUsersValid(UsersList users, num distance)
    {
      //**************** Initialisation  ****************//
      List&lt;String&gt; _listValidUsersName = [];
      UsersList _filteredUsers = UsersList();
  
      //**************** Script  ****************//
      users.forEach((user) // ERROR =&gt; The method 'forEach' isn't defined for the type 'UsersList'. 
      {
        if(distance &lt;= 100 &amp;&amp; !_listValidUsersName.contains(user.name))
        {
          _listValidUsersName.add(user.name);
        }
      });
  
      if(_listValidUsersName.length &gt; 0)
      {
        users.forEach((user) // ERROR =&gt; The method 'forEach' isn't defined for the type 'UsersList'. 
        {
          if (_listValidUsersName.contains(user.name))
          {
            if (_filteredUsers.length &gt; 0 &amp;&amp; _filteredUsers.last.name == user.name)
            // ERROR =&gt; The getter 'length' isn't defined for the type 'UsersList'
            // ERROR =&gt; The getter 'last' isn't defined for the type 'UsersList'
            {
              _filteredUsers.removeLast();
              _filteredUsers.add(user);
            }
            else {
              _filteredUsers.add(user);
            }
          }
        });
      }
  
      if (_filteredUsers.length &lt;= 0)
      {
        throw MyException.noData();
      }
  
      return _filteredUsers;
    }
  </code></pre>