首页 > 解决方案 > 无法无条件访问属性“xyz”,因为接收者可以为“null”

问题描述

我已将我的颤振项目版本升级到最新版本(2.5.3)。然后由于不受支持而发生了一些错误null-safety。我已经修复了很多但仍然没有任何想法来修复以下null-safety错误,

代码

itemBuilder: (context, suggestion) {
 return ListTile(
          subtitle: Text(
            suggestion.stateA == null || suggestion.stateB == null
                ? '${suggestion.city}, ${suggestion.country}'
                : '${suggestion.city}, ${suggestion.state}, ${suggestion.country}',
            style: Theme.of(context).textTheme.bodyText,
          ),
        );
 }
  

错误

The property 'stateA' can't be unconditionally accessed because the receiver can be 'null'.
The property 'stateB' can't be unconditionally accessed because the receiver can be 'null'.
The property 'city' can't be unconditionally accessed because the receiver can be 'null'.
The property 'country' can't be unconditionally accessed because the receiver can be 'null'.

如何在代码上方添加检查空安全性?

我已经尝试使用!&?运算符,但它对我不起作用

(即suggestion!.state

标签: flutterdartmigrationnull-safety

解决方案


推荐阅读