My code is losing state when I switch from one screen to another in flutter app

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật