Search code examples
androidflutterdartflutter-webdart-null-safety

The value 'null' can't be assigned to the parameter type 'Offset' because 'Offset' is not nullable


I've been following a tutorial on youtube for flutter. https://www.youtube.com/watch?v=Zv5T2C1oKus. I'm new to flutter and dart btw. I don't understand this error message.

lib/main.dart:91:36: Error: The value 'null' can't be assigned to the parameter type 'Offset' because 'Offset' is not nullable.

  • 'Offset' is from 'dart:ui'. points.add(null); ^

here is my code

error comes from "points.add(null)"

enter image description here


Solution

  • In the video you linked, there is a check whether points[x] is null, so I assume the elements of points should be nullable. You can achieve this with

    List<Offset?> points = [];
    

    (instead of List<Offset> points = [];). The added questionmark makes it possible for elements of the list to be either a instance of the class Offset or the value null.