首页 > 解决方案 > 在 Flutter 中使用缓存网络图像的原生“AssertionError_throwNew”

问题描述

当我决定将缓存的图像放在网络图像的位置时,我正在开发我的应用程序,因为效果更好。我做到了。但是我的图像不再可以点击了。当我点击全屏时,图像只有白色,并显示你们在“errors_patch.dart”的标题(原生“AssertionError_throwNew”;)上看到的错误。我敢打赌是因为缓存的网络图像与 PhotoView idk 内的 imageProvider 中的仅网络图像混合。在此先感谢,这是我的代码(为了帮助您更多,我将容器也放在图像所在的位置):

    final urlImages1 = [
     'https://i.imgur.com/Y3UejT0.jpg',
     'https://i.imgur.com/KNFL3qd.jpg',
     'https://i.imgur.com/fxAH9HY.jpg',
     'https://i.imgur.com/9GkgdKx.jpg',
    ]; 

    Widget buildImage(String urlImage01, int index) => Container(
    margin: EdgeInsets.symmetric(horizontal: 5),
    child: ClipRRect(
      borderRadius: BorderRadius.circular(20),
      child: GestureDetector(
        child: Hero(
          tag: 'imageHero',
          child: CachedNetworkImage(
            imageUrl: urlImage01,
            imageBuilder: (context, imageProvider) => Container(
              width: 350,
              height: 405,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                image: DecorationImage(
                    image: imageProvider, fit: BoxFit.cover),
              ),
            ),
            placeholder: (context, url) => SizedBox(
              width: 100,
              height: 100,
              child: Center(
                  child: CircularProgressIndicator(
                valueColor:
                    AlwaysStoppedAnimation<Color>(Colors.pink.shade700),
              )),
            ),
            errorWidget: (context, url, error) => Icon(Icons.error),
          ),
        ),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (_) {
                return Scaffold(
                  extendBodyBehindAppBar: true,
                  appBar: AppBar(
                    elevation: 0,
                    backgroundColor: Colors.transparent,
                    leading: backIcon(),
                  ),
                  body: GestureDetector(
                    child: Center(
                      child: Hero(
                        tag: 'imageHero',
                        child: PhotoView(
                          backgroundDecoration:
                              BoxDecoration(color: Colors.black87),
                          minScale: PhotoViewComputedScale.contained * 0.9,
                          maxScale: PhotoViewComputedScale.covered * 2,
                          imageProvider: NetworkImage(
                            urlImage01,
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              },
            ),
          );
        },
      ),
    ),
  );

                                          Container(
                                            width: 405,
                                            child: ClipRRect(
                                              borderRadius:
                                                  BorderRadius.only(
                                                topLeft:
                                                    Radius.circular(20),
                                                topRight:
                                                    Radius.circular(20),
                                              ),
                                              child: Column(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .center,
                                                children: [
                                                  CarouselSlider.builder(
                                                    itemCount:
                                                        urlImages1.length,
                                                    itemBuilder: (context,
                                                        index, realIndex) {
                                                      final urlImage01 =
                                                          urlImages1[index];
                                                      return buildImage(
                                                          urlImage01,
                                                          index);
                                                    },
                                                    options:
                                                        CarouselOptions(
                                                      height: 300,
                                                      enlargeStrategy:
                                                          CenterPageEnlargeStrategy
                                                              .height,
                                                      enlargeCenterPage:
                                                          true,
                                                      onPageChanged: (index,
                                                              reason) =>
                                                          setState(() =>
                                                              currentIndex =
                                                                  index),
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    height: 10,
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ),

标签: imageflutterexception

解决方案


更新,伙计们,我是对的。这真的是我认为是错误的地方。所以只要改变这个粒子:

                    child: PhotoView(
                      backgroundDecoration:
                          BoxDecoration(color: Colors.black87),
                      minScale: PhotoViewComputedScale.contained * 0.9,
                      maxScale: PhotoViewComputedScale.covered * 2,
                      imageProvider: NetworkImage(
                        urlImage01,
                      ),
                    ),

对此:

                      child: PhotoView(
                          backgroundDecoration:
                              BoxDecoration(color: Colors.black87),
                          minScale: PhotoViewComputedScale.contained * 0.9,
                          maxScale: PhotoViewComputedScale.covered * 2,
                          imageProvider: CachedNetworkImageProvider(
                            urlImage01,
                          ),
                        ),

我做到了。所以不用担心。问题是我无法将此答案标记为已解决。仅在 2 天内


推荐阅读