Reputation: 891
I am converting an old app from using a riverpod provider to using a riverpod 2.0 notifier provider. I have converted all of the code but still get an error in one file.
I have a constructor, UsersNotifier.fromFirestore(Map<String, dynamic> firestore), with an initializer list that I need to fix but I am so new to flutter I don't know how.
Every field in the initializer list has an error similar to this error: 'cellPhone' isn't a field in the enclosing class.
I am calling the fromFirestore from another class here:
Users users = Users.fromFirestore(
(usersStreamProvider.data! as QuerySnapshot).docs[index]
.data() as Map<String, dynamic>);
So you can see that I am passing a Map<String, dynamic> to the constructor.
Here is the code for the Notifier class:
class UsersNotifier extends Notifier<Users> {
final firestoreService = FirestoreService();
final companyRef = FirebaseFirestore.instance.collection(('Company'));
final userDB = FirebaseFirestore.instance.collection(('users'));
// **************************************************
@override
Users build() {
return Users( // Return the initial state
userId: '',
fName: '',
lName: '',
address1: '',
address2: '',
city: '',
state: '',
zipCode: '',
cellPhone: '',
officePhone: '',
email: '',
companyId: '',
companyName: '',
mlsId: '',
mls: '',
);
}
fromFirestore(Map<String, dynamic> firestore)
: cellPhone = firestore['cellPhone'],
address1 = firestore['address1'],
address2 = firestore['address2'],
company = firestore['company'],
companyId = firestore['companyId'],
userId = firestore['userId'],
city = firestore['city'],
fName = firestore['fName'],
lName = firestore['lName'],
officePhone = firestore['officePhone'],
state = firestore['state'],
zipcode = firestore['zipCode'],
mls = firestore['mls'],
mlsId = firestore['mlsId'],
email = firestore['email'];
}
}
Here is the Users class which is in the same file:
class Users {
String? userId;
String? fName;
String? lName;
String? address1;
String? address2;
String? city;
String? state;
String? zipCode;
String? cellPhone;
String? officePhone;
String? email;
String? companyId;
String? companyName;
String? mlsId;
String? mls;
Users(
{this.userId,
this.fName,
this.lName,
this.address1,
this.address2,
this.city,
this.state,
this.zipCode,
this.cellPhone,
this.officePhone,
this.email,
this.companyId,
this.companyName,
this.mlsId,
this.mls});
Users copyWith({
String? userId,
String? fName,
String? lName,
String? address1,
String? address2,
String? city,
String? state,
String? zipCode,
String? cellPhone,
String? officePhone,
String? email,
String? companyId,
String? companyName,
String? mlsId,
String? mls,
}) {
return Users(
fName: fName ?? this.fName,
lName: lName ?? this.lName,
address1: address1 ?? this.address1,
address2: address2 ?? this.address2,
city: city ?? this.city,
state: state ?? this.state,
zipCode: zipCode ?? this.zipCode,
cellPhone: cellPhone ?? this.cellPhone,
officePhone: officePhone ?? this.officePhone,
email: email ?? this.email,
companyId: companyId ?? this.companyId,
companyName: companyName ?? this.companyName,
mlsId: mlsId ?? this.mlsId,
mls: mls ?? this.mls,
);
}
}
As you can see I have declared it as a type Users so how do I fix this error and give the constructor access to the Users data fields?
Thanks
Upvotes: 0
Views: 31
Reputation: 891
I declared all of the elements in the initialization list like this:
String? userId,
String? fName,
String? lName,
String? address1,
String? address2,
String? city,
String? state,
String? zipCode,
String? cellPhone,
String? officePhone,
String? email,
String? companyId,
String? companyName,
String? mlsId,
String? mls,
I put this right after the Users build(). All of the errors went away but I don't know if this is the correct thing to do.
Is this the correct thing?
Upvotes: 0