[Flutter/Dart💠]I read a valuable article about `null safety`

Dart

I feel hazy about 'null safety'

When I review the code written by my collegue by using Dart(Flutter), I came across the late keyword.

late Widget something;

I wonder that using late is good practice or not...

I was just searching about late keyword. And hit the best article which is written about it

You can judge whether you want to make sure that The context ensure to initialize a late valiable or not.

It is generaly said that ...

you should not use optional variables too much!!

Because unimportant optional valiables will make your code increase verbosely.

For instance, you just defined the optional valiable and you have known that it will have never been null.

SomeClass? a ;

a = SomeClass();

a?.var ....// verbosely

a!.var...// Good

Forced unwrap has a merit.

You can notice the fatal error when doing develop process.

If you write the code with unimportant optinal valiables, you missed fatal error and the code will fall through without error even outputting error log.If you use forced unwrapping and error happens, The proggram will crash and you can notice it immediately.

It can say same things in late. Even if you fail to initialize variable with late, you may come across the error with crash of application.

And, If you build as a release mode, Flutter will not crash the application and catch the bugs.

コメント

タイトルとURLをコピーしました