Dart: Unexpected text 'late'

Viewed 7125

Going through this tutorial on Riverpod and using this code gives an error Unexpected text 'late'

class Clock extends StateNotifier<DateTime> {

  Clock() : super(DateTime.now()) {
    _timer = Timer.periodic(Duration(seconds: 1), (_) {
      state = DateTime.now();
    });
  }

  late final Timer _timer;

  @override
    void dispose() {
      _timer.cancel();
      super.dispose();
    }
}

My code is exactly the same as found on the linked website. This error looks strange as I haven't found anything on google or stackoverflow similar to this.

1 Answers

late is for projects converted to null safety using min dart sdk 2.12. It tells the compiler that it's null now but will be initialized later on. You can either omit the late keyword in that case or change the min sdk in your pubspec.yaml to 2.12.

Related