flutter 参数“image”的值不能为“null”,因为其类型不同,但隐式默认值为“null”

pieyvz9o  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(267)

我尝试通过构造函数传递图像.但是我不想添加'required',因为我不需要在整个列表中使用它
完整错误- '参数'image'的值不能为'null',因为其类型不同,但隐式默认值为' null '。请尝试添加显式非' null '默认值或'required'修饰符'
创建构造函数的代码

class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Image.asset(image),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

这是我试图传递LvPopup( image: 'assets/images/blue_dot.png', users_info: "In the process", title: 'Status', ),的代码
我试着添加一个问号,就像这个“final String”图像;',然后在image.asset(image as String)中,那么上面的这个错误就消失了,并且出现了一个新的错误'Type Null不是类型转换中类型'string'的子类型'。

oxf4rvwz

oxf4rvwz1#

class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String? image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Image.asset(image ?? ""),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String? image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Image.asset(image!),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

如果image为空,则第一个命令将Image.asset(image)设置为空字符串,而第二个命令将告诉dart image不为空

  • 编辑:* 要解决图像出现在其他位置的问题,请使用此
class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String? image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  image != null ? Image.asset(image!) : Container (),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

相关问题