s k
s k

Reputation: 5192

Why I can't use nullable type in flutter

I have a flutter project created in Jan 2019. I read an article in Dart and says that it support nullable type.

class c {
  int? id;
}

But I can't get the above to compile. It give some error message but has nothing to do with nullable type. Seems like it doesn't understand the '?' at all.

Error: enter image description here

May I know what might have missing in my code? Do I need to upgrade my Dart or Flutter? I looked at pubspec.yaml, but can't find any where that reference to dart.

Upvotes: 0

Views: 450

Answers (1)

Luiz Filipe Medeira
Luiz Filipe Medeira

Reputation: 1310

You don't need to use null safety now, the official support for flutter should arrive at the beginning of next year, according to the flutter team, it is not yet ready for production. But if you just want to test it, update your sdk, sdk: "> = 2.7.0 <3.0.0"

You need to create an analysis_options.yaml file in the root of your flutter app and write something like

analyzer:
  enable-experiment:
    - non-nullable

And also make sure you have a recent enough version of flutter

flutter upgrade

Michael Thomsen from Dart made this post today, where it explains more about null safety in Flutter. https://medium.com/flutter/null-safety-flutter-tech-preview-cb5c98aba187

Upvotes: 5

Related Questions