Unable to access Riverpod provider state

0 votes

I currently have a riverpod provider declared as follows:

@riverpod
class FeeInputData extends _$FeeInputData {
  @override
  Fee build() {
    return const Fee(
      id: 0,
      institutionId: 'nmb7588BVjhvnb',
      name: '',
      academicYearId: 'utf896DHGFoIO7t87',
      installments: [],
      totalAmount: 0,
    );
  }

  void updateName(String data) {
    state = state.copyWith(name: data);
  }
}

I have used the riverpod generator which generates an AutoDisposeNotifierProvider.

This is my Consumer widget which has a ListView.seperated as its child:

Consumer(
                builder: (context, ref, _) {
                  final installments = ref.watch(feeInputDataProvider).installments;
                  return ListView.separated(
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: installments.length,
                    separatorBuilder: (context, index) => Column(
                      children: [
                        SizedBox(height: SizeConfig(context).getVerticalSize(10)),
                        Row(
                          children: [
                            const Expanded(child: Divider(color: AppColors.kSubtitle, height: 0.5)),
                            SizedBox(width: SizeConfig(context).getHorizontalSize(5)),

                            /// Remove address button
                            AddRemoveButton(
                              type: AddRemoveButtonType.remove,
                              onTap: () {
                                ref.read(feeInputDataProvider.notifier).removeInstallment(index);
                              },
                            ),
                          ],
                        ),
                      ],
                    ),
                    itemBuilder: (context, index) => InstallmentWidget(
                      installment: installments[index],
                    ),
                  );
                },
              ),

Now, the issue is that when I try to access the provider via Consumer widget, it returns the following error:

The following JSNoSuchMethodError was thrown building Consumer(dirty, dependencies:
[UncontrolledProviderScope], state: _ConsumerState#08454):
TypeError: Cannot read properties of undefined (reading 'prototype')

I have wrapped my MaterialApp with ProviderScope widget.

How do I fix this?

Apr 17, 2023 in Flutter by sarit
• 1,830 points
505 views

1 answer to this question.

0 votes

The error message you're seeing suggests that there might be an issue with the way you're accessing the FeeInputData provider state in the updateName method.

One possible cause of this issue is that you're trying to access the provider state outside of a ProviderScope widget, which could result in the state being undefined.

To fix this, ensure that you wrap the relevant part of your widget tree with a ProviderScope widget, like so:

ProviderScope(
  child: YourWidget(),
)

Another potential issue is that you're not initializing the FeeInputData provider correctly. You might want to try creating a new instance of the FeeInputData class, and then pass it to the ProviderScope widget as a provider:

ProviderScope(
  providers: [
    Provider((ref) => FeeInputData()),
  ],
  child: YourWidget(),
)

Once you've done this, you should be able to access the provider state using the ref.watch method, as you're already doing in the Consumer widget.

If you're still having trouble, it might be helpful to provide more context around how you're using the FeeInputData provider and how it's being initialized in your app.

answered Apr 17, 2023 by Anitha

Related Questions In Flutter

0 votes
1 answer

How to change Android minSdkVersion in Flutter Project?

Yes, you can change the minSdkVersion in ...READ MORE

answered Mar 21, 2023 in Flutter by Tej
7,253 views
0 votes
1 answer

How to make a TextField with HintText but no Underline?

You can achieve this by using the ...READ MORE

answered Mar 21, 2023 in Flutter by Anushi
1,354 views
0 votes
1 answer

How to Determine Screen Height and Width?

To optimize your Flutter application for different ...READ MORE

answered Mar 21, 2023 in Flutter by tan
485 views
0 votes
1 answer

Sizing elements to percentage of screen width/height

Yes, you can size an element relative ...READ MORE

answered Mar 24, 2023 in Flutter by vishalini
940 views
0 votes
1 answer

What is the future of flutter?

Hi@MD, Flutter is a rather new cross-platform framework ...READ MORE

answered Jul 17, 2020 in Others by akhtar
• 38,230 points
904 views
0 votes
1 answer

What is Flutter?

Hi@akhtar, Flutter is an app SDK for building ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,440 points
1,067 views
0 votes
1 answer

How to install Flutter in Windows system?

Hi@akhtar, You can follow the below-given steps to ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,440 points
1,514 views
0 votes
1 answer
0 votes
1 answer

How to change flutter project name and id?

Yes, it is possible to change the ...READ MORE

answered Mar 20, 2023 in Flutter by pooja
2,583 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP