FlutterProgramming

2 Proven Ways to Disable Null Safety in Flutter

Looking for How to Disable Null Safety in Flutter? You’ve come to the right place. In this article, I will provide the answer to this question.

Why we use Null Safety in Flutter?

Null safety is a hot topic in the Dart and Flutter community and for good reason. It’s an important feature that can help you write safer, more stable code. But when does null safety come into play in Dart Flutter? Let’s take a closer look.

In Dart, null safety is enabled by default as of version 2.12.0. This means that if you’re using a version of Flutter that’s at least 2.12.0, you can take advantage of null safety without having to do anything special. However, if you’re using a newer version of Flutter, you’ll need to remove null safety manually. Here is the perfect guide for you.

How to Disable Null Safety in Flutter

1st Method: Downgrading the SDK constraints

To disable null safety, you need to modify the SDK version to a version below 2.12.0, specifically 2.11.0. Refer to the following code for a better understanding:

YAML
environment:
  sdk: ">=2.11.0 <3.0.0"

Next, execute the below command to obtain the configuration of the Dart version 2.11.0:

 
dart pub get

To disable null checks for a particular file, you can specify at the top of your Dart file.

Dart
// @dart=2.9

For better comprehension, I am sharing my page_one.dart file. Upon executing the dart pub get command, this line will be automatically added at the top. However, if it is not added, you will need to manually add it yourself.

Dart
// @dart=2.9

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class Page1 extends StatelessWidget {
  const Page1({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text("Page 3"),
      ),
    );
  }
}

If your code is in the current version of Dart, disabling null safety may result in a Dart analyzer error, indicating that null safety is disabled successfully.

Related Flutter Posts,

2nd Method: Directly Run without Null Safety

Note: Programs with mixed versions run on unsound null safety, which may lead to null reference errors at runtime. However, this is only possible if a null or nullable type escapes from a null-unsafe library and enters the null-safe code.

Also Read:  The Right Way to Convert Hex to Flutter Color

To turn off null safety, use the –no-sound-null-safety flag while running the dart or flutter command.

In case you own a dart project, you can turn off null safety and run it as follows.

dart --no-sound-null-safety run

In case you own a Flutter project, you can execute Flutter without null safety by following these steps.

flutter run --no-sound-null-safety

Conclusion

In conclusion, removing null safety in flutter can be a viable solution for developers who prefer to take a more cautious approach to their programming.

While null safety is a useful feature that can help prevent runtime errors, it may not always be necessary depending on the project’s specific requirements.

Whatever your preference may be, keep in mind that the process of disabling null safety is straightforward and can be completed in just a few simple steps. You can explore this article for more information about null safety.

I hope you liked this article on How to Disable Null Safety in Flutter, If you’re interested in more articles on Flutter technology, we have plenty to offer. Please feel free to leave any questions related to this topic in the comments section below.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button