简体   繁体   English

我无法解决 null 检查运算符用于 null 值错误

[英]I can't resolve null check operator used on a null value error

Uint8List? _file;

SizedBox(
                  height: 45,
                  width: 45,
                  child: AspectRatio(
                    aspectRatio: 487 / 451,
                    child: Container(
                      decoration: BoxDecoration(
                          image: DecorationImage(
                              image: MemoryImage(_file!),
                              fit: BoxFit.fill,
                              alignment: FractionalOffset.topCenter)),
                    ),
                  ),
                ),

Null check operator used on a null value. Null 检查运算符用于 null 值。 I know the reason for this error but I couldn't figure out how to solve it.我知道这个错误的原因,但我不知道如何解决它。

With Uint8List?使用 Uint8List? the value I defined as 'This value may be null', with _file.我使用 _file 定义为“此值可能为空”的值。 I'm trying to call it with 'This value is not null'.我试图用“这个值不为空”来调用它。 This is the cause of the error but I couldn't figure out how to fix it.这是错误的原因,但我不知道如何解决它。

It would be better to do a null check 1st.最好先进行 null 检查。

if (_file != null)
  SizedBox(
    height: 45,
    width: 45,
    child: AspectRatio(
      aspectRatio: 487 / 451,
      child: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                image: MemoryImage(_file!),
                fit: BoxFit.fill,
                alignment: FractionalOffset.topCenter)),
      ),
    ),
  ),

Or或者

image: _file != null
    ? DecorationImage(
        image: MemoryImage(_file),
        fit: BoxFit.fill,
        alignment: FractionalOffset.topCenter,
      )
    : null,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM