Home > OS >  How can I disable null safety in flutter?
How can I disable null safety in flutter?

Time:03-24

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!

CodePudding user response:

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.

CodePudding user response:

You can declare the variables using var or dynamic and now the compiler won't check the variable type.

var answerText;
dynamic answerColor;

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

And if your try to build without the null safety checking then use this comment line // @dart=2.9 in the main.dart first line.

// @dart=2.9

import 'package:flutter/material.dart';

void main() async {
  
  runApp(
    const MyApp()
  );
}

CodePudding user response:

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.

  • Related