1

In my Flutter app, I have this ChangeNotifier provider. On the homepage a ChangeNotifier is created.

ChangeNotifierProvider<UserTeam>(create: (_) => UserTeam()),

I am setting the value for this provider on button press using this code below.

late UserTeam _userTeam;
_userTeam = Provider.of<UserTeam>(context);
UserTeam _userTeam = UserTeam(userDetails: test, pageDetails: 3);
_userTeam.setUserTeam(_userTeam);

I am passing this ChangeNotifier provider to the bottom sheet.

showModalBottomSheet<dynamic>(
          isScrollControlled: true,
          shape: const RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(30), topRight: Radius.circular(30))),
          backgroundColor: const Color(0xFFFFFFFF),
          context: context,
          builder: (BuildContext context2) {
            return BlocProvider.value(
              value: BlocProvider.of<AuthenticationBloc>(context),
              child: BlocProvider.value(
                value: BlocProvider.of<AuthBloc>(context),
                child: MultiProvider(
                  providers: [
                    ChangeNotifierProvider<UserTeam?>(create: (_) => _userTeam),
                  ],
                  child: StatefulBuilder(builder: (BuildContext context, StateSetter mystate) {
                    return SingleChildScrollView(
                      child: SizedBox(
                        height: MediaQuery.of(context).size.height - 50,
                        width: MediaQuery.of(context).size.width,
                        child: const teamSearch(),
                      ),
                    );
                  }),
                ),
              ),
            );
          });

When opening the bottom model sheet for first time, ChangeNotifier UserTeam is available. After closing and opening it's getting disposed and throwing an error as this code shows below.

Another exception was thrown: A UserTeam was used after being disposed.

Please help me with this.

Kirby
  • 15,127
  • 10
  • 89
  • 104
fazilSizzlers
  • 195
  • 10

1 Answers1

1

The code is confusing Provider on which variable to use, please change variable names

final userTeam = Provider.of<UserTeam>(context);
UserTeam _userTeam = UserTeam(userDetails: 'test, pageDetails: 3);
userTeam.setUserTeam(_userTeam);

Rather than creating a whole new Provider later in showModalBottomSheet create a value provider

ChangeNotifierProvider.value<UserTeam?>(value: _userTeam),
Usama Karim
  • 1,100
  • 4
  • 15
  • Hello usama, many thanks for the response. let me brief it again. I have userteam page which set the value for changenotifier, once value is set changenotifier shows user page. In user page i have button to open bottomsheet, in bottom sheet also i can access the changenotifier value. after closing and reopening the bottomsheet changenotifier is not available. – fazilSizzlers Aug 24 '22 at 09:38
  • Is the solution above is not working? – Usama Karim Aug 24 '22 at 10:19
  • No it's not changenotifier provider getting disposed from the userteam page when i am opening and closing the modelsheet – fazilSizzlers Aug 24 '22 at 12:57
  • There has to be more code included to get the idea about what's wrong. – Usama Karim Aug 25 '22 at 05:34
  • Thanks for the response. I solved this by passing function as parameter to bottommodelsheet. function parameter contains changenotifier model class with value, and am returning the function with value back to the main page. – fazilSizzlers Aug 25 '22 at 20:07