Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad state: No ProviderScope found in flutter consumer component

Tags:

flutter

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);
});
like image 704
Dolphin Avatar asked Dec 27 '20 03:12

Dolphin


People also ask

How do you deal with bad state no element?

Bad state no element found error in flutter# firstWhere((e)=> e. age == 2); in the above code if where condition doesn't find any data matching age =2 then it will give you Bad state no element found error . for avoiding this error you can add orElse parameter which will return if element not found.

What is ProviderScope in flutter?

ProviderScope class Null safety. A widget that stores the state of providers. All Flutter applications using Riverpod must contain a ProviderScope at the root of their widget tree. It is done as followed: void main() { runApp( // Enabled Riverpod for the entire application ProviderScope( child: MyApp(), ), ); }


1 Answers

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

void main() {
   runApp(ProviderScope(child: MyApp()));
}
like image 81
Nikolay Avatar answered Oct 17 '22 05:10

Nikolay