【问题标题】:Exception has occurred. _TypeError (type 'Container' is not a subtype of type 'String')发生异常。 _TypeError(“容器”类型不是“字符串”类型的子类型)
【发布时间】:2021-01-21 06:07:52
【问题描述】:

我正在尝试在 CircleAvatar() 中显示用户图像 URL;即使我提供了它,它也显示一个空错误。还是我做错了?

首先我调用 Auth Methods 和 Set image String img,

  AuthMethods _auth = AuthMethods();
  String img;

然后我在 initstate 中实例化

    _auth.getCurrentUser().then((user) async {
      setState(() {
        user.uid;

        img = user.photoUrl

      });
    });
  }

然后在Drawer()

中调用
  DrawerHeader(
    child: CircleAvatar(
      backgroundColor: Colors.deepOrange,
      radius: 100,
      child: Image(
        fit: BoxFit.cover,
        image: NetworkImage(
              img != null ? img : Container(),  // <== here
            ) ??
            CircularProgressIndicator(),
      ),
    ),
    decoration: BoxDecoration(
      color: Colors.orange,
    ),
  ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    NetworkImage 将字符串作为边界,而不是小部件。 在你的情况下:

    NetworkImage(
                  img != null ? img : Container(),  // <== here
                )
    

    如果 img 即 String 为空,则返回 Container(),但你应该返回一个 String(也许你可以给一个空字符串)

    【讨论】:

      【解决方案2】:

      NetworkImage 在其字符串参数中采用图像 URL,在您的情况下,您将图像 URL 或小部件传递到此参数中,显然小部件不是字符串。你的空检查应该像这样在 NetworkImage 之上:

      ...
      
      image: 
         img != null ? NetworkImage(img) 
                     :
                  CircularProgressIndicator(),
       //Or container or different desired widget in case image path is null
      ...
      
      

      【讨论】:

        猜你喜欢
        • 2020-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 2020-08-09
        • 2020-12-14
        相关资源
        最近更新 更多