错误状态:在颤振消费者组件中找不到 ProviderScope

Dol*_*hin 5 flutter

当我像这样在颤振中添加颤振消费者组件时:

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,);
                              },
                            );
                          },
                        ),
                      )
Run Code Online (Sandbox Code Playgroud)

显示此错误:

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.
====================================================================================================
Run Code Online (Sandbox Code Playgroud)

现在我fish-redux用来管理我的颤振状态,我应该使用ProviderScope吗?我该怎么做才能解决这个问题?这是storiesTypeProvider

final storiesTypeProvider = FutureProvider.family((ref, type) async {
  return await Repo.getArticles(type);
});
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您使用 flutter_riverpod。您需要将 ProviderScope 添加到 main() 函数。像这样:

void main() {
   runApp(ProviderScope(child: MyApp()));
}
Run Code Online (Sandbox Code Playgroud)

  • 我已经有“ProviderScope”了,但它不起作用。但是当我将“ProviderScope”移动到“MyApp()”中并包装“MaterialApp”时,它就起作用了。 (3认同)