I have a flutter app where a new user is taken to the user profile screen. Here they can enter more information and save the data.
Just before the save method, I put all the data into a riverpod NotifierProvider and pass this data to the save method. This works well.
After the new user information has been saved, they are taken to a company profile screen where they can enter all the data about their company.
After the company has been saved, I retrieve the company document ID and I want to save it to the user record so they are linked.
However, sometime between the time the new user is saved and the company is saved, I lose all the data in the user NotifierProvider. I don’t get any errors before I try to save the company ID in the user record.
Both screens extend ConsumerState, if that helps.
I have been able to narrow it down to the following code:
I am passing the user data to the save method
ref.read(usersNotifierProvider.notifier).
saveUser(ref.read(globalsNotifierProvider), ref.read(usersNotifierProvider),);
This is the method:
saveUser(globals, user) async {
if (ref.watch(globalsNotifierProvider).newUser == true) {
// final DocumentSnapshot currentCompanyProfile =
final newUser = Users(
userId: user.userId,
fName: user.fName,
lName: user.lName,
mls: user.mls,
mlsId: user.mlsId,
address1: user.address1,
address2: user.address2,
city: user.city,
userState: user.userState,
zipCode: user.zipCode,
cellPhone: user.cellPhone,
officePhone: user.officePhone,
companyId: globals.companyId,
companyName: user.companyName,
email: user.email,
businessType: user.businessType,
deviceToken: user.deviceToken,
);
firestoreService.saveNewUser(toMap(newUser));
ref.read(globalsNotifierProvider.notifier).updatenewUser(false);
} else {
final DocumentSnapshot currentuserProfile =
await userDB.doc(user.UserId).get();
var newUser = Users(
userId: ref.read(globalsNotifierProvider).currentUid,
fName: (state.fName != null && state.fName != "")
? state.fName
: currentuserProfile.get('fName'),
lName: (state.lName != null && state.lName != "")
? state.lName
: currentuserProfile.get('lName'),
address1: (state.address1 != null && state.address1 != "")
? state.address1
: currentuserProfile.get('address1'),
address2: (state.address2 != null && state.address2 != "")
? state.address2
: currentuserProfile.get('address2'),
city: (state.city != null && state.city != "")
? state.city
: currentuserProfile.get('city'),
userState: (userState != null && userState != "")
? userState
: currentuserProfile.get('state'),
/*state: (globals.selectedState != null)
? globals.selectedState
: globals.currentuserState,*/
zipCode: (state.zipCode != null && state.zipCode != "")
? state.zipCode
: currentuserProfile.get('zipCode'),
cellPhone: (state.cellPhone != null && state.cellPhone != "")
? state.cellPhone
: currentuserProfile.get('cellPhone'),
officePhone: (state.officePhone != null && state.officePhone != "")
? state.officePhone
: currentuserProfile.get('officePhone'),
companyId: companyId,
companyName: (state.companyName != null && state.companyName != "")
? state.companyName
: currentuserProfile.get('Company'),
mlsId: ref.read(globalsNotifierProvider).mlsId,
mls: (state.mls != null && state.mls != "")
? state.mls
: currentuserProfile.get('mls'),
deviceToken: (state.deviceToken != null && state.deviceToken != "")
? state.deviceToken
: currentuserProfile.get('deviceToken'));
businessType:
ref.read(globalsNotifierProvider).userBusinessType;
firestoreService.saveUser(
newUser, ref.read(globalsNotifierProvider).currentUserId!);
}
This is where I do the actual save:
Future<void> saveNewUser(users) {
return _db.collection('users').add(users);
}
When I come back from saving the data, the provider is empty.
Thanks
0
I removed this line and it worked:
firestoreService.saveNewUser(toMap(newUser));
ref.read(globalsNotifierProvider.notifier).updatenewUser(false); <<<< REMOVED