找不到正确的供应商<>英雄动画跳动

我在我的Gallery文件中推送一个新的Route到带有英雄动画的Details文件中。一切都很好,但当我调用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);
                }));
            },
          ),
...
}

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

为什么我在这里得到一个提供商错误和如何修复这个?

谢谢你的帮助

# # #当使用Hero widget it seems 小部件似乎有一个Context issue.

问题。

这意味着当动画执行时,在目的地调用Provider。 oflt;Model>(context)时,它将无法找到正确的Provider。

你只需要做你会做的一个导航器,在你的例子中给出:

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。

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码