title: Flutter mapping data to model and using it
body: <p>I am very new to flutter. I am having this problem and this function is inside the Widget Build</p>
  <pre><code>Dashboard user = Dashboard();
    Future&lt;Dashboard&gt; setup() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      id = prefs.getInt('id');
      final response = await get('http://localhost/myproject/dashboard/user/$id');
      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        user = Dashboard.fromJson(data);
      }
      return user;
    }
  </code></pre>
  <p>My model is:</p>
  <pre><code>class Dashboard {
    int id;
    String username;
    Profile profile;
  
    Dashboard(
        {this.id,
        this.username,
        this.profile,
   });
  
    Dashboard.fromJson(Map&lt;String, dynamic&gt; json) {
      id = json['id'];
      username = json['username'];
      profile = json['profile'] != null ? new Profile.fromJson(json['profile']) : null;
  
    }
  
    Map&lt;String, dynamic&gt; toJson() {
      final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
      data['id'] = this.id;
      data['username'] = this.username;
      if (this.profile != null) {
        data['profile'] = this.profile.toJson();
      }
      return data;
    }
  }
  
  class Profile {
    String birthday;
    int age;
    Null image;
    String gender;
    Profile(
        {this.birthday,
        this.age,
        this.image,
        this.gender});
  
    Profile.fromJson(Map&lt;String, dynamic&gt; json) {
      birthday = json['birthday'];
      age = json['age'];
      image = json['image'];
      gender = json['gender'];
    }
  
    Map&lt;String, dynamic&gt; toJson() {
      final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
      data['birthday'] = this.birthday;
      data['age'] = this.age;
      data['image'] = this.image;
      data['gender'] = this.gender;
      return data;
    }
  }
  </code></pre>
  <p>My widget:</p>
  <pre><code>Container(
   padding: const EdgeInsets.only(left: 10, right: 10),
            child: Column(
                children: [
                   Container(
                      child: (user.profile.age != null)
                         ? Text(
                              user.profile.age.toString(),
                              textAlign: TextAlign.center,
                              style: TextStyle(
                              fontSize: 12.0,
                              fontFamily: 'Open-Sans-Regular',
                              color: Colors.black,
                              ),
                              )
                         : Text(
                             '36',
                             textAlign: TextAlign.center,
                             style: TextStyle(
                             fontSize: 12.0,
                             fontFamily: 'Open-Sans-Regular',
                             color: Colors.black,
                              ),),
                         ),
  </code></pre>
  <p>Stacktrace :</p>
  <pre><code>The getter 'age' was called on null.
  Receiver: null
  Tried calling: age
  
  </code></pre>
  <p>and I am getting this error:
  NoSuchMethodError: The getter 'age' was called on null.</p>
  <p>I initialized the class properly..
  I used <a href="https://javiercbk.github.io/json_to_dart/" rel="nofollow noreferrer">https://javiercbk.github.io/json_to_dart/</a> for auto generating a model.
  What I am doing wrong? Please enlighten me.</p>