Dolphin
Dolphin

Reputation: 38975

Bad state: No ProviderScope found in flutter consumer component

When I add flutter consumer component in flutter like this:

SliverPadding(
                        padding: const EdgeInsets.symmetric(vertical: 8.0),
                        sliver: Consumer(
                              (context, read) {
                            return read(storiesTypeProvider(articleRequest)).when(
                              loading: () {
                                // return SliverFillRemaining(
                                // child: Center(child: CircularProgressIndicator()));
                                return SliverToBoxAdapter(child: Center(child: LoadingItem()));
                              },
                              error: (err, stack) {
                                print(err);
                                return SliverToBoxAdapter(
                                    child: Center(child: Text('Error: $err')));
                              },
                              data: (ids) {


                                //return StoryList(ids: ids,storiesType: articleRequest.storiesType,);
                              },
                            );
                          },
                        ),
                      )

shows this error:

flutter: [debug] Capture from onError 'package:flutter/src/widgets/nested_scroll_view.dart': Failed assertion: line 806 pos 14: 'position.hasContentDimensions && position.hasPixels': is not true.
flutter: [debug] Capture from onError 'package:flutter/src/widgets/nested_scroll_view.dart': Failed assertion: line 806 pos 14: 'position.hasContentDimensions && position.hasPixels': is not true.

======== Exception caught by widgets library =======================================================
The following StateError was thrown building RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#0ac5b](state: RawGestureDetectorState#734b4(gestures: <none>, behavior: opaque)):
Bad state: No ProviderScope found

The relevant error-causing widget was: 
  SmartRefresher file:///Users/dolphin/Documents/GitHub/cruise-open/lib/src/page/home/components/homelistdefault_component/view.dart:47:22
When the exception was thrown, this was the stack: 
#0      ProviderStateOwnerScope.of (package:flutter_riverpod/src/framework.dart:216:7)
#1      _ConsumerState.didChangeDependencies (package:flutter_riverpod/src/consumer.dart:107:46)
#2      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4839:11)
#3      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4655:5)
...     Normal element mounting (4 frames)
...
====================================================================================================

======== Exception caught by scheduler library =====================================================
'package:flutter/src/widgets/nested_scroll_view.dart': Failed assertion: line 806 pos 14: 'position.hasContentDimensions && position.hasPixels': is not true.
====================================================================================================

======== Exception caught by scheduler library =====================================================
'package:flutter/src/widgets/nested_scroll_view.dart': Failed assertion: line 806 pos 14: 'position.hasContentDimensions && position.hasPixels': is not true.
====================================================================================================

now I am using fish-redux to manage my flutter state, should I must using ProviderScope? what should I do to fix this problem? this is the storiesTypeProvider:

final storiesTypeProvider = FutureProvider.family((ref, type) async {
  return await Repo.getArticles(type);
});

Upvotes: 26

Views: 17210

Answers (2)

Ayoub belkacemi
Ayoub belkacemi

Reputation: 21

i searched for the solution and i dont get it on youtube and on chatgpt and here on stackoverflow ,i tryed to see flutter_riverpod version on pub.dev and i see it flutter_riverpod: ^2.3.6 ,,me i use flutter_riverpod: ^2.1.3 before upgrading to flutter_riverpod: ^2.3.6

just upgrade your ( flutter_riverpod ) to the latest now its flutter_riverpod: ^2.3.6 this fix me the problem

Upvotes: 2

Nikolay
Nikolay

Reputation: 773

If you use flutter_riverpod. You need to add ProviderScope to the main() function. Like this:

void main() {
   runApp(ProviderScope(child: MyApp()));
}

Upvotes: 53

Related Questions