DEV Community

Cover image for Starting with Flutter: Sound nullsafety
TheOtherDev/s
TheOtherDev/s

Posted on

Starting with Flutter: Sound nullsafety

Flutter 2 has landed few months ago and one of the key feature of the release were Null Safety.

Null safety it's an huge upgrade, to start migrating, inside the pubspec.yaml file, change the sdk minimum to 2.12.0:

environment:
  sdk: ">=2.12.0<3.0.0" 
Enter fullscreen mode Exit fullscreen mode

then in the terminal run the following commands to regenerate the package configuration file:

  • flutter clean
  • flutter pub get

Now you'll have pretty much all the code broken, don't worry (or maybe yes :)), it's normal. Let's see now how your world will change:

Dependencies

You can still use non-null safety dependencies in your project but you should absolutely consider to update them as soon as possible. If a dependency needs dart SDK < 2.12 it will be not usable anymore. To check on updates and which package needs to do implement null safety you can use this command:

dart pub outdated --mode=null-safety
Enter fullscreen mode Exit fullscreen mode

You will see if your packages are null safety ready or if they need some updates.

"required"

You are already accustomed to the @required keyword which will create a warning if a parameter is missing from a constructor, right? The new required keyword workd pretty similarly but it will show an error on compiler so you'll never miss a "required" anymore.

"?" & "!"

Meet new "friends", the question mark and exclamation mark! The first one will label your class parameters as nullable and will warning you any time you try to use them without checking in if they are null. You can force-use a nullable parameter by adding a ! after the name of it. Also the ? will change how you will create classes. Let's see an example:

class MyButton extends StatelessWidget {
  const MyButton ({
    Key? key,
    required this.content,
    required this.onTap,
    this.color = azure,
    this.manualPadding,
  }) : super(key: key);

  final String content;
  final Function() onTap;
  final Color color;
  final EdgeInsets? manualPadding;

  @override
  Widget build(BuildContext context) {
    //MY build...
  }
}
Enter fullscreen mode Exit fullscreen mode

In this MyButton class we can see that "content" and "onTap" are required, "color" is not and "manualPadding" is nullable. This means:

  • content and onTap should always be valorized or the compiler will show an error.
  • color is a parameter which, if not valorized while creating MyButton, will have "azure" as a color.
  • manualPadding is a nullable parameter, so while creating our class we can absolutely set it as null.

When you implement null safety also many main widgets' parameters can be set as null and you will receive warnings and errors by the compiler.

"late"

The late keyword can be used if you are SURE it will be valorized before using it because the compiler will not stop you to use it, even if it is null (and in this case it will throw an error while running). Also it will show you a warning if you try to check if it is null as... It SHOULD NOT BE NULL!

late List<String> allStrings;
Enter fullscreen mode Exit fullscreen mode

Wrapping all up

Null safety is not very easy to digest but will improve your app performance and awareness to possible issues with your code. It can be very scary at the beginning but you will go through it. And if you're not ready... No hassle! You can still stick to non-null safey!

Want to check more awesome Flutter tutorials? Click here!

Top comments (0)