【问题标题】:Could not find the correct Provider<> with Hero animation Flutter使用英雄动画 Flutter 找不到正确的 Provider<>
【发布时间】:2021-11-13 04:51:45
【问题描述】:

我正在将画廊文件中的新Route 推送到带有Hero 动画的详细信息文件。一切都很好,但是当我在详细信息文件中调用 pop() 以返回画廊时,我有一个 Provider 错误:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following ProviderNotFoundException was thrown building GalleryThumbnail(dirty, state:
flutter: _GalleryThumbnailState#9dc96):
flutter: Error: Could not find the correct Provider<GalleryViewModel> above this GalleryThumbnail Widget

这是我的画廊文件:

@override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: galleryViewModel),
          ChangeNotifierProvider.value(value: galleryProvider)
        ],
        child: Scaffold(
            resizeToAvoidBottomInset: false,
            body: Stack(
              children: [
                Consumer<GalleryViewModel>(
                    builder: (context, model, child) =>
                        galleryViewModel.assetsList.length != 0 ? gallery() : emptyGallery()),

...

}


Widget gallery() {
    return Container(
      padding: EdgeInsets.only(left: 5, right: 5, top: 5),
      child: CupertinoScrollbar(
        child: GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 2,
              mainAxisSpacing: 2,
              childAspectRatio: 0.85,
            ),
          itemCount: galleryViewModel.assetsList.length,
          padding: EdgeInsets.only(bottom: Constants.navBarSize),
          physics: BouncingScrollPhysics(),
          shrinkWrap: true,
          itemBuilder: (context, index) {
            if(index >= galleryViewModel.assetsList.length-30) {
              galleryViewModel.onLoadMore(index);
            }
            return _galleryItem(index);
          },

        ),
      ),
    );
  }


Widget _galleryItem(int index) {
    return Stack(
      children: [
        Consumer<GalleryProvider>(
          builder: (context, model, child) {
            return Container(
              padding:
                  multipleSelection ? EdgeInsets.all(2) : EdgeInsets.all(0),
              decoration: multipleSelection ? BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      border: Border.all(color: Colors.transparent , width: 2)) : BoxDecoration(),
              child: child,
            );
          },
            Hero(
            createRectTween: (Rect? begin, Rect? end) {
              RectTween _rectTween =
              RectTween(begin: begin, end: end);
              return _rectTween;
            },
            tag: galleryViewModel.assetsList[index].id,
            child:
          child: GalleryThumbnail(
            asset: galleryViewModel.assetsList[index],
           
            onTap: (data) {
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return MediaEditorPage(
                      asset: galleryViewModel.assetsList[index],
                      bytes: data);
                }));
            },
          ),


...

}

当我在没有英雄或其他路线的情况下推送时没有错误,我不知道为什么在英雄上方找不到提供者,导航器转换时我不会通知任何提供者。

为什么我在此处收到提供程序错误以及如何解决此问题?

感谢您的帮助

【问题讨论】:

    标签: flutter dart provider navigator flutter-change-notifier


    【解决方案1】:

    使用Hero 小部件时,似乎存在Context 问题。

    这意味着当动画正在执行时,并且在您调用Provider.of&lt;Model&gt;(context) 的目的地,它不会找到正确的Provider

    您只需要像使用 Navigator 那样做,在您的示例中给出:

    Hero(
      createRectTween: (Rect? begin, Rect? end) {
        RectTween _rectTween =
        RectTween(begin: begin, end: end);
        return _rectTween;
      },
      tag: galleryViewModel.assetsList[index].id,
      child: ChangeNotifierProvider.value(
        value: galleryViewModel,
        child: GalleryThumbnail(
          asset: galleryViewModel.assetsList[index],
          size: size,
          onLongPress: () {
            if (!multipleSelection) {
              multipleSelection = true;
              galleryViewModel.selectedAssets.add(galleryViewModel.assetsList[index]);
              galleryProvider.notify();
            }
          },
          onTap: (data) {
            Navigator.push(context, MaterialPageRoute(builder: (context) {
                      return MediaEditorPage(
                          asset: galleryViewModel.assetsList[index],
                          bytes: data);
                    }));
          },
        ),
      ),
    )
    

    我添加了:ChangeNotifierProvider.value(value: galleryViewModel) 包装了GalleryThumbnail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 2021-09-29
      • 2021-07-16
      • 2020-05-15
      • 2021-06-23
      • 2021-04-29
      • 2021-07-25
      • 2021-05-31
      相关资源
      最近更新 更多