title: Using model class in flutter (dart)
body: <p>I'm trying to create a model class with a few methods to simplify retrieving information from Firebase. </p>
  
  <p>I have a "Unregistered" page where user clicks on the "Login with google" button. If my user is already registered it checks the DB to see if he completed his profile, otherwise it writes basic data to the database with a field <code>profileCompleted: false</code> and redirects the user to the profile page so he can complete his profile. </p>
  
  <p>I'm trying to use a model here, but i'm getting all sorts of errors (just started learning flutter/dart). </p>
  
  <p>I'll share some code, let me know if its not enough!</p>
  
  <p>Unregistered page.</p>
  
  <pre><code>    if (user != null) {
        await prefs.setString('id', user.uid);
        // Check is already sign up
        final QuerySnapshot result = await Firestore.instance
            .collection('users')
            .where('id', isEqualTo: user.uid)
            .getDocuments();
        final List&lt;DocumentSnapshot&gt; documents = result.documents;
  
        if (documents.length == 0) {
          debugPrint("User not in DB ?");
          //debugPrint(userInfo.toString());
          // Update data to server if new user
  
          Firestore.instance.collection('users').document(user.uid).setData({
            'id': user.uid,
            'nickname': user.displayName,
            'photoUrl': user.photoUrl,
            'email': user.email,
            'createdAt': DateTime.now(),
            'provider': user.providerId,
            'profileCompleted': false,
            'bloodType': 'A',
            'rhType': 'Negativ',
            'donatedBefore': 'Nu',
            'lastDonation': '0'
          });
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =&gt; CompleteProfile(
                        currentUserId: user.uid,
                        userInfo: documents.single,
                      )));
        } else if (documents.single["profileCompleted"] == false) {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =&gt; CompleteProfile(
                        currentUserId: user.uid,
                        userInfo: documents.single,
                      )));
        } else {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =&gt; HomePage(
                        currentUserId: user.uid,
                        userInfo: documents.single,
                      )));
        }
      }
  </code></pre>
  
  <p>Complete profile page</p>
  
  <pre><code>class CompleteProfile extends StatefulWidget {
    final String currentUserId;
    final userInfo;
    static const routeName = '/profile';
    final User user;
  
    CompleteProfile(
        {Key key,
        this.title,
        @required this.currentUserId,
        @required this.userInfo,
        this.user})
        : super(key: key);
  
    final String title;
  
    @override
    _CompleteProfileState createState() =&gt; _CompleteProfileState(user,
        currentUserId: this.currentUserId, userInfo: this.userInfo);
  }
  
  class _CompleteProfileState extends State&lt;CompleteProfile&gt; {
    User user;
  
    _CompleteProfileState(this.user,
        {Key key, @required this.currentUserId, @required this.userInfo});
    final String currentUserId;
    final userInfo;
  </code></pre>
  
  <p>My issue here is that if i try to set a dropdown like this -> </p>
  
  <pre><code>child: ListTile(
  title: DropdownButton&lt;String&gt;(
  items: _bloodTypes.map((String value) {
  return DropdownMenuItem&lt;String&gt;(
  value: value, child: Text(value));
  }).toList(),
  style: textStyle,
  value: retrieveBloodType(user.bloodType),
  onChanged: (value) =&gt; updateBloodType(value),
  </code></pre>
  
  <p>And on default value-> </p>
  
  <pre><code>  String retrieveBloodType(String value) {
      return _bloodType;
    }
  </code></pre>
  
  <p>I get an error that i'm getting on null.</p>
  
  <p>My idea was to initialize the <code>User</code> model with something like <code>User user = new User(userInfo["id"],userInfo["email"]....</code> in which <code>userInfo</code> is an object retrieved from firebase. However, that throws another error that only static members can be accessed in initalizers.</p>
  
  <p>My model is quite simple so here's a sneak peek (few lines, not the entire model). </p>
  
  <pre><code>class User {
    int _id;
    String _nickname;
    int get id =&gt; _id;
    String get nickname =&gt; _nickname;
    set bloodType(String newBloodType) {
      if (newBloodType.length &lt;= 50 &amp;&amp; newBloodType != null) {
        _bloodType = newBloodType;
      }
    }
  </code></pre>
  
  <p>Any idea on how id have to change this to work? The only reason i'm thinking of using models is to simplify screen widgets (so i can add all firebase logic to the model instead of the widgets).</p>