属性 'polylinePoints' 无法无条件访问,因为接收者可以是 'null'

The property 'polylinePoints' can't be unconditionally accessed because the receiver can be 'null'

    polylines: {
      if (_info != null)
        Polyline(
          polylineId: PolylineId('overview_polyline'),
          width: 5,
          points: _info.polylinePoints
              .map((e) => LatLng(e.latitude, e.longitude))
              .toList(),
        ),
    },

我在行 _info.polylinPoints 上遇到了一个错误,但我确实在此之前做了一个条件语句,说明 if (_info != null) 。我知道为什么我仍然收到错误消息吗?

即使您检查 _info 不为 null,该类型以后也不会自动提升,因为它不是局部变量(我无法通过查看这段代码来确认这一点,但这相当常见问题)。因此,编译器仍然认为该变量可能包含可为空的值。

由于您知道存在空检查并且 _info 不会为空,因此您可以轻松地使用 !运算符:

polylines: {
  if (_info != null)
    Polyline(
      polylineId: PolylineId('overview_polyline'),
      width: 5,
          points: _info!.polylinePoints // <-- Notice the ! operator
              .map((e) => LatLng(e.latitude, e.longitude))
              .toList(),
        ),
    },

查看更多信息和上下文 here