【问题标题】:error: The return type 'NetworkImage' isn't a 'Widget', as required by the closure's context错误:根据闭包上下文的要求,返回类型“NetworkImage”不是“Widget”
【发布时间】:2021-03-08 11:24:33
【问题描述】:
import 'package:carousel_pro/carousel_pro.dart';
import '../models/post_model.dart';

Carousel(
        animationDuration: null,
          images: [
               ListView.builder(
                      itemCount: posts[index].MediaImage.length,
                          physics: ScrollPhysics(),
                             itemBuilder: (context, index) {
                                  return NetworkImage(posts[index].MediaImage[index].image);
                                }, ) ],) ,

我正在尝试显示来自网络的多张图片。 有人知道我为什么会收到这个错误吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    在您的 itemBuilder 中返回以下内容

    return Image.network(posts[index].MediaImage[index].image);
    

    【讨论】:

      【解决方案2】:

      您不能在轮播中使用 ListView.builder,为什么还要使用它??

      Carousel 期望 NetworkImage 仅作为 url 字符串,在其中放置任何其他小部件都会导致您出现此错误。

      0

      import 'package:carousel_pro/carousel_pro.dart';
      import '../models/post_model.dart';
      
      Carousel(
              animationDuration: null,
                images: [NetworkImage(posts[i].MediaImage[i].image.toString(),],) ,
      

      这是正确的方法,.toString(), 通常是必需的,因为 NetworkImage 需要一个字符串。

      如果您有多个图像,请使用:

      for(int i =0;i<="please specify your length here", i++);
      

      我添加了i 来代替索引。

      Carousel 从不直接使用除 AssetImage 或 NetworkImage 之外的任何其他 Widget。

      【讨论】:

        【解决方案3】:

        Flutter 命名事物的方式有点令人困惑,但 NetworkImageImageProvider,而不是 Widget(它实际上显示了图像)。您想要的小部件是Image,它可以从ImageProvider 构造,也可以直接通过其Image.network constructor 从URL 构造。

        【讨论】:

          猜你喜欢
          • 2021-10-29
          • 2021-05-04
          • 2020-10-14
          • 2021-10-30
          • 1970-01-01
          • 2022-01-12
          • 2021-08-26
          • 2021-06-27
          • 2021-07-08
          相关资源
          最近更新 更多