Null safety in Dart - Introduction
Why Stack Overflow recommends it
Stack Overflow users link it when answering Dart and Flutter errors caused by null safety, such as the ? and ! operators and nullable argument type mismatches.
Recommended by 3 people across 3 different months, 2020 to 2022.
What it is
A short official introduction from the Flutter team to Dart's null safety feature, published while it was in beta in November 2020. It explains why null safety is valuable and covers the basics, including the ? syntax for nullable types.
What people said
Recommends Lee3 · 2020-11 · Stack Overflow · on “Question mark after function parameter in dart”
As of Dart 2.12, the language supports null safety. Here, ? explicitly states that a variable/parameter may be null. Learn more here [LINK] and watch this [this video] video
Recommends Bach · 2021-08 · Stack Overflow · on “The argument type 'String"?"' can't be assigned to the parameter type 'String'”
@MSameer This is a feature introduce with Dart 2.0, kind of like a revolution change that make Dart a null-safe language, so the only way to not using it is to convert Dart back to 1.0. The `?` is to indicate that the variable can be null. My recommendation is to learn using this feature instead of turning it off, since it'll help us a lot in avoiding potential bugs with null values. There's a quite nice video here from Flutter team: [this video]
Recommends eHarazi · 2022-09 · Stack Overflow · on “What does the exclamation mark mean before a function call?”
The variable chunkCallback must be able to accept null and you cannot call a function on a nullable type without either using ! or ? . This is part of Darts sound null safety Some great videos on this: Flutter vid [this video] [LINK] Even though the conditional statement checks for null, Dart still requires the exclamation mark before the function call. The difference between using ! over ? is that it will throw an exception instead of using the variable if the value is null. Some examples: class Car { String? make; // String or null type Car([this.make]); // parameter is optional } main() { Car test = Car('Ford'); // initialised with a value Car test2 = Car(); // no value given so null is defaul
Cites Yihao Gao · 2020-12 · Stack Overflow · on “How is Dart "sound null-safety" different from Kotlin null safety?”
This Dart official video [this video] states that Dart's so-called "sound null safety" is better than Kotlin's null safety design, because it can optimise the code based on whether a variable is declared nullable, and other languages (I assume this refers to languages including Kotlin) have to do runtime checks to ensure null safety. So, what extra optimization does Dart do? How does it interoperate with legacy codebases that are not null-safe (written before null safety) while ensuring null safety?
Cites Zac · 2021-05 · Stack Overflow · on “Error:The method '[]' can't be unconditionally invoked because the receiver can be 'null'”
What's happening is that after switching to null safety, you cannot directly write statements using variables that can be null, without checking if they're null. In this case, the variable snapshot.data can be null, so you have to write code accordingly. Try converting your code to this: return ListView.builder( scrollDirection: Axis.horizontal, itemCount: snapshot.data!['interest'].length, // i am getting error here itemBuilder: (context, index) { return Padding( padding: const EdgeInsets.only(top: 12.0), child: bottomCardList( 'assets/1 (6).jpeg', snapshot.data?['Interest'][index] // i am getting error here .toString(), ), ); }, ); Now, with this, your error for itemCount should vanish (if
Cites Baker · 2021-07 · Stack Overflow · on “The argument type 'double?' can't be assigned to the parameter type 'double'. dart(argument_type_not_assignable)”
You could also null check the local variables, thereby making your code null safe: when location changes if (lat/lon are not null) { animate camera } So something like this might work: void _onMapCreated(GoogleMapController _cntlr) { _controller = _cntlr; _location.onLocationChanged.listen((l) { if (l.latitude != null && l.longitude != null) { _controller.animateCamera( CameraUpdate.newCameraPosition( CameraPosition( target: LatLng(l.latitude, l.longitude), zoom: 15, ), ), ); } }); } Logically, it wouldn't make sense to animate to a null latitude/longitude, so you can skip that listener call altogether if that's the case. Filip talks about this situation & handling here [this video] .
Cites MaNDOOoo · 2021-09 · Stack Overflow · on “Flutter: null safety updates get me some errors in code”
You can use a null-able value like String? _token . Because it can be a null value before initializing the value. Or you can use a late keyword to init later, but you shouldn't use this before init. For more info, reference the Null safety in Dart video [this video] .
Cites user18520267 · 2022-04 · Stack Overflow · on “Error in initializing AnimationController in flutter”
From around July 2020 Dart was becoming null safe. After that we can't keep variables uninitialized without explicitly specifying that they could be null or that they would be initialized later. Solution 1 - Using late keyword Replace AnimationController _controller with late AnimationController _controller Late variables are used in two situations as per Dart documentation [LINK] : Declaring a non-nullable variable that’s initialized after its declaration. Lazily initializing a variable. Solution 2 - Using ? operator ? is called Operational spread operator in category of Null check operators Replace AnimationController _controller with AnimationController? _controller; ? essentially means t