【问题标题】:The argument type 'Object' can't be assigned to the parameter type 'ImageProvider<Object>'参数类型“Object”不能分配给参数类型“ImageProvider<Object>”
【发布时间】:2021-06-08 04:26:51
【问题描述】:

我刚刚更新到 Dart2 和 Flutter sdk: '>=2.12.0

 decoration: new BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.blueAccent,
              border: Border.all(
                  color: Colors.blueAccent,
                  width: 20.0,
                  style: BorderStyle.solid),
              image: new DecorationImage(
                fit: BoxFit.cover,
                image: myMarkerThumb != 'noImage'
                    ? NetworkImage(myMarkerThumb)
                    : AssetImage('assets/images/noImageAvailable.png'),
              ),
            ),

参数类型“Object”不能分配给参数类型“ImageProvider”。 ),

我刚开始用颤振,不知道在哪里看。

【问题讨论】:

    标签: flutter if-statement dart-2


    【解决方案1】:

    嘿,这是我在颤振 repo 中使用 dart 2.12 打开的issue

    您可以在此期间做出的一个简单解决方法就是投射对象。

    
     decoration:  BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.blueAccent,
                  border: Border.all(
                      color: Colors.blueAccent,
                      width: 20.0,
                      style: BorderStyle.solid),
                  image:  DecorationImage(
                    fit: BoxFit.cover,
                    image: myMarkerThumb != 'noImage'
                        ? NetworkImage(myMarkerThumb)
                        : AssetImage('assets/images/noImageAvailable.png') as ImageProvider,
                  ),
                ),
    
    

    【讨论】:

    • 在将导航器分配给变量并使用 Map 弹出上下文时,我也遇到了同样的情况。例如,无法使用普通的 Map.from(object) 方法解码此 Map,因为它会给出类似的错误。相反,您需要将响应转换为 Map。
    • 无论如何,非常感谢您的解决方法
    【解决方案2】:

    已打开问题中的某个人解决了投射到图像提供者的问题

    @AbdurrahmanElrayes

    这个解决方案也适用于我

    image: DecorationImage( 
       image: true ? NetworkImage('someNetWorkLocation.com') : AssetImage('assets/images/noImageAvailable.png') as ImageProvider 
    ),
    

    【讨论】:

    • 在 FileImage 的情况下也可以解决。例如显示由 imagePicker 选取的图像,我们可以使用 cast 作为图像提供者来显示它
    • 是的,它对我有用。只需在 AssetImage 的末尾添加“as ImageProvider”即可。
    • 太好了,你节省了我的时间
    猜你喜欢
    • 2022-08-18
    • 2023-02-10
    • 1970-01-01
    • 2023-02-18
    • 2022-08-19
    • 2021-11-20
    • 2021-09-30
    • 2021-12-24
    • 2021-08-27
    相关资源
    最近更新 更多