0

I'm working on a quiz app... and this null safety thing is giving me some errors, for example I can not write:

Answer({this.answerText, this.answerColor});

without an error: The parameter 'answerColor' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.

I allready tried to change the environment: sdk: version... and it kinda worked... but that gave me a new error... once I change the version to 2.11.0 (I have 2.15.1) a new error comes up: "This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.12.0 or higher, and running 'pub get'." in this part of the code:

class TrigoEjercicios extends StatefulWidget const TrigoEjercicios({Key? key}) : super(key: key);

on the question mark... this error is in every .dart file I have on in my proyect So, what can I do? I apologise if this is not clear enough... is my first time here... if you need more information please let me know!

Pablo López
  • 1
  • 1
  • 1
  • This seems a bad idea. Better try to fix the error. Here the error message tells you exactly what to do. – Patrick Mar 23 '22 at 16:30
  • Just put question mark where you have declared your variable like String ? answerText ; – Vishal Agrawal Mar 23 '22 at 16:38
  • At some point you're going to need to deal with null-safety because packages and examples will not be updated for the old, null-unsafe path, so you will no longer get bug fixes, compatibility fixes, or other new features. Dealing with null-safety earlier is easier than later since presumably your application will only grow in size, and you therefore will have more code to migrate and fix later. – jamesdlin Mar 23 '22 at 18:02

2 Answers2

2

I agree with @Patricks comment. Null safety is not the problem here, (and I don't mean this in a rude way), but your lack of understanding of null safety is.

If you want to continue building Flutter apps in the future you just need to learn this, no way around it. Null safety is a valuable addition to Dart and you don't want to be left behind.

Say you have this class that used to work but now it's giving you an error.

class Answer {
  Answer({this.answerText, this.answerColor}); // error on this constructor

  final String answerText;
  final Color answerColor;
}

You have a few options. You can make both parameters required, which removes any possibility of a null value because the compiler will force you to pass in a value.

class Answer {
  Answer({required this.answerText, required this.answerColor}); // no more error

  final String answerText;
  final Color answerColor;
}

Or, if there is a scenario the the values can actually be null, then simply make the parameters nullable with a ?.

class Answer {
  Answer({this.answerText, this.answerColor}); // no more error

// these are now nullable
  final String? answerText; 
  final Color? answerColor;
}

Or provide default values

class Answer {
  Answer({this.answerText = 'correct', this.answerColor = Colors.blue});

  final String answerText;
  final Color answerColor;
}

Either one of those will solve the error you mentioned in your post. If you decide to properly convert your app to null safety (you should) then you can expect to go through these errors one by one and make them null safe. It might be tedious, but its worth it.

Another common error you'll probably see is:

a value of String? can't be assigned to a value of String

String here could be any data type, it's just an example.

That is where the bang operator comes into play and you simply add a ! to whatever value you're trying to pass in to tell the compiler that you're sure it won't be null.

There's no shortage of info online about null safety in Dart. Any error you encounter has already been asked about and answered on here, guaranteed. I suggest you just take the time and learn it.

Loren.A
  • 4,872
  • 1
  • 10
  • 17
  • A third option is to provide non-null default arguments (e.g. `Answer({this.answerText = '', this.answerColor = Color(0)})`). – jamesdlin Mar 23 '22 at 18:03
  • Good call, I spaced on that one. Answer updated. – Loren.A Mar 23 '22 at 18:06
  • Thanks a lot... yes indeed... I'm new in this flutter stuff... you helped a lot! thanks again – Pablo López Mar 23 '22 at 22:14
  • While these answers work, the suggestion regarding making the values nullable is not a wise one. The only place this really makes sense is where the application meets the outside world. After that, it should either be a default value or be an Either type (e.g. Either). Further, the use of String and Color types is an anti-pattern called Primitive Obsession. – Bill Turner Oct 05 '22 at 21:17
  • @Bill Turner sometimes fields can have valid reasons to be nullable, depending on the scenario. The purpose of the answer is to show all options. It's up to the end user to decide whether there is a justifiable reason for a field to be nullable or not. – Loren.A Oct 05 '22 at 22:14
  • @Loren.A I don't want to get into a flame war, here. I did make clear you can expect nulls coming from external interactions. That is a "sometimes". However, once accepted by the app, I see little reason to pass a null around. Using Either, possibly with a Void type, or Option is a better choice. It forces you to think of what you have and guarantees what it is you do have. Allowing nulls for a type is a risk. And, yes, it always depends. It is a choice, but you better have thought through why it is necessary when there are better choices that make illegal state unrepresentable. – Bill Turner Oct 06 '22 at 16:47
1

But if you really want to get rid of null safety than what you can do is you can run flutter project without null safety with --no-sound-null-safety option with flutter run.

Also you can add this as an argument in launch.json in VSCode

"configurations": [
        {
            "name": "Flutter",
            "request": "launch",
            "type": "dart",
            "flutterMode": "debug",
            "args": [
                "--no-sound-null-safety"
            ],
        },
]

This is just an alternative but this is not recommended by any developer.

Manishyadav
  • 1,361
  • 1
  • 8
  • 26