flutter 抛出一个错误,无法加载资产,我不希望有任何图像

h7wcgrx3  于 2023-01-31  发布在  Flutter
关注(0)|答案(2)|浏览(113)

我在传递图像构造函数时遇到了一个问题。我传递了一个图像到我想要的地方,当我不想要它的时候,它会自动添加一些东西到剩下的地方。
我试着从这里传球

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(
      title: 'E-mail',
      users_info: 'gmail.com',
    ),
    SizedBox(
      height: 5,
    ),
    LvPopup(
      users_info: 'Johny Bravo',
      title: 'Name and Surname',
    ),
    SizedBox(
      height: 5,
    ),
    LvPopup(
      image: 'assets/images/blue_dot.png',
      users_info: "In the process ",
      title: 'Status',
    ),
    SizedBox(
      height: 5,
    ),
    LvPopup(
      users_info: '+0.00',
      title: 'Earnings(USD)',
    ),

但问题是我只想在以下位置获得图像:

LvPopup(
  image: 'assets/images/blue_dot.png',
  users_info: "In the process ",
  title: 'Status',
),

但它抛出一个错误,其余的,它说无法加载资产,当我不想传递图像到其余的LvPopup只到该“状态”
下面是一个图像:
err img

bakd9h0s

bakd9h0s1#

如果图像为空,则显示SizedBox()

Row(
        children: [
          image != null ? Image.asset(image) : SizedBox(),
          Padding(
            padding: const EdgeInsets.all(5.0),
            child: Text(
              users_info,
            ),
          ),
        ],
      )
goqiplq2

goqiplq22#

如果变量image为null,则在Image.assets(image ?? "")行中会出现错误,如果您尝试沿着路径显示一个带有空字符串的图像,则会出现错误,最好尝试以下操作

image==null ?  null :  Image.assets(image)

相关问题