Victor Guijarro
Victor Guijarro

Reputation: 53

Add new property to the default User class in flutter

I am new in flutter and I am developing an app where I want to have Users with custom properties. I would like to use the default User class since it has methods and properties I need. But I don't how to add new properties (for example; City).

The sign-up is currently working and I am adding the information to CloudFirestore in this way:

Future<void> addUser(User user){
    //create a CollectionReference to add a new user
    CollectionReference usersCollectionReference = FirebaseFirestore.instance.collection('users');
    return usersCollectionReference.doc(user.uid)
        .set({
      'user_uid':user.uid,
      'email':user.email,
    })
        .then((value) => print('user added to cloud firestore'))
        .catchError((onError) => print('Failed to add user to cloud Firestore: $onError'));

  }

So, my question is how could I custom properties to that user?

Thanks

Upvotes: 0

Views: 723

Answers (1)

Stefano Amorelli
Stefano Amorelli

Reputation: 4854

One option would be to create a parent class, that contains all the desired properties, and the reference to the User instance:

import 'package:firebase_auth/firebase_auth.dart';

class Account {
  User user;
  String city;
  Account({this.user, this.city}); // example constructor
}

By doing so, it's possible to access both the User class functionality, and all the additional properties/functions defined in the Account class:

Account account = new Account(user: user, city: 'Washington');
print(account.user.displayName);
print(account.city);

Upvotes: 1

Related Questions