r/FlutterDev • • Nov 19 '20

Article Announcing Dart null safety beta

https://medium.com/flutter/announcing-dart-null-safety-beta-4491da22077a
107 Upvotes

19 comments sorted by

22

u/Zalack Nov 19 '20

Really cool. null safety is one of the things I really like about rust. It's nice to see other languages looking at similar guard rails.

7

u/eibaan Nov 19 '20

I'm already using NNBD for all server-side code and all non-productive Flutter code (maintaining a nullability branch there). Migration was (mostly) easy and it feels so much better. I really miss Swift's if let, though, and hope that this get's added to the language quickly. I try to keep 3rd party dependencies to an absolute minimum, but I'd love to have a NNBD'd provider version and a NNBD'd Firebase API.

12

u/neddstarkk Nov 19 '20

Hey, beginner dev here. I would really like to know why is null safety so important.

24

u/Baul Nov 19 '20

I call it my billion-dollar mistake…At that time, I was designing the first comprehensive type system for references in an object-oriented language. My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. – Tony Hoare, inventor of ALGOL W.

Effectively, null makes it way easier to write code that can crash. Devs trained to deal with null know to always check if an argument passed into a function is null or not, but you have to remember to write code to check it, otherwise it can blow up unexpectedly.

By making nullable types explicit to the compiler, you can force the dev to unwrap potentially null objects before using them, or let the dev explicitly say that a function requires a non-null object instead.

At the end of the day, it's about reducing confusion and giving the compiler one more way to check that the dev didn't make a fatal mistake.

If you're interested in learning more, this is a great writeup of both "why" and "how" dart is making types null-safe: https://dart.dev/null-safety/understanding-null-safety

4

u/azuredown Nov 19 '20

There are numerous examples from the article under the heading 'The benefits of sound null safety'. In short: less crashes (and other problems such as cache misses) due to more errors caught at runtime, smaller code size, and possibly some compiler optimization (although these are currently theoretical).

3

u/Tobi-As Nov 19 '20

This short video gives a good explanation. https://youtu.be/iYhOU9AuaFs

1

u/chocolate_chip_cake Nov 19 '20

I like to think it avoids crashes, i'm new too!

3

u/azuredown Nov 19 '20

It's finally happening!!!

2

u/kaiserkarel Nov 20 '20

Finally! Our stack consists of flutter and a rust backend, and having more compiler checks on the frontend side as well really lowers the cognitive overhead.

1

u/poq106 Nov 19 '20

If i use this in my project can I use libs from pub.dev that are not migrated?

1

u/eibaan Nov 19 '20

Yes, mostly. Currently, there's a version conflict with master and the latest version of firebase_auth but in general, it works. You must provide --no-sound-null-safety to your flutter command line tools.

1

u/shreyasb2001 Nov 20 '20

Nice to see that dart had covered this. (Only other language I know that had implemented this is swift)

1

u/[deleted] Nov 19 '20 edited Nov 19 '20

[removed] — view removed comment

2

u/KalilPedro Nov 20 '20

AngularDart is gonna support nnbd, the team announced it. And dart2js and dartdevc support nnbd already

-5

u/[deleted] Nov 20 '20

[removed] — view removed comment

5

u/lets4r Nov 20 '20

How can someone that works with code not know how to say "variable" correctly...?

This is so contemptuous and unrespectful to say something like this. It's very hard to speak a language that it's not our first language and even when it's their first language, some people can have some issues to articulate some words.

What do you want to achieve by saying something like this? Showing you're somewhat better than him?
Imagine he read this message and put you in his shoes, how would you feel?

Have a good day nonetheless.

3

u/Shewebi Nov 20 '20

Nobody cares