Search code examples
flutterdart-null-safety

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(),
        ),
    },

I got an error on the line _info.polylinPoints, but i did make a conditional statement prior to this stating if (_info != null) . My I know why I am still getting the error message?


Solution

  • Even though you check for the _info not being null, the type is not automatically promoted later since it's not a local variable (I cannot confirm this by looking at this piece of code, but that's quite a common problem). Thus, the compiler still considers for the variable to possibly hold a nullable value.

    Since you are aware that the null check is there and _info won't be null, you could easily use the ! operator:

    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(),
            ),
        },
    

    Check for more information and context here.