簡體   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 檢查運算符用於 null 值。 我知道這個錯誤的原因,但我不知道如何解決它。

使用 Uint8List? 我使用 _file 定義為“此值可能為空”的值。 我試圖用“這個值不為空”來調用它。 這是錯誤的原因,但我不知道如何解決它。

最好先進行 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)),
      ),
    ),
  ),

或者

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