Questions tagged [dart-null-safety]

Tag for questions regarding Dart code involving non-nullable types. This should only be used in combination with the Dart tag.

With the announcement of null safety, there are two types of Dart code:

  • Code with only nullable types.
  • Code with null safety enabled → with non-nullable types.

The tag covers questions involving the latter. Question with this tag should be subset of .

1185 questions
212
votes
17 answers

Cannot run with sound null safety because dependencies don't support null safety

I have followed "Enabling null safety" on dart.dev and also migrated my whole Flutter application to null safety. Now, I am trying to run it using flutter run. However, it will not start because of the following error: Error: Cannot run with sound…
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
185
votes
13 answers

Null check operator used on a null value

I am new to Flutter I got this error when I run my simple flutter APP. I could not figure out why this error occurred. Error Null check operator used on a null value My code in main.dart import 'package:flutter/material.dart'; import…
Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45
167
votes
20 answers

The argument type 'Function' can't be assigned to the parameter type 'void Function()?' after null safety

I want to achieve to make a drawer with different items on it, so I am creating a separate file for the DrawerItems and the with the constructor, pass the data to the main file. But I get the following error on the onPressed function: "The argument…
dosytres
  • 2,096
  • 3
  • 10
  • 21
131
votes
8 answers

The parameter can't have a value of 'null' because of its type in Dart

Dart function I have the following Dart function and I am now using null safety: void calculate({int factor}) { // ... } The analyzer complains that: The parameter 'factor' can't have a value of 'null' because of its type, and no non-null…
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
102
votes
2 answers

What is Null Safety in Dart?

I have heard of the new Dart null safety language feature (NNBD), currently the "'non-nullable' experiment". It is supposed to introduce non-nullable by default. The feature specification can be found here and the language GitHub issue here. How…
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
68
votes
5 answers

Non-nullable instance field must be initialized

class Foo { int count; // Error void bar() => count = 0; } Why I'm seeing an error when I'am already initializing it in the bar method? I could understand this error if count was marked final.
iDecode
  • 22,623
  • 19
  • 99
  • 186
58
votes
4 answers

The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'

Why List() constructor is not accessible after Dart's null safety? // Compile time error: 'List' is deprecated and shouldn't be used. // The default 'List' constructor isn't available when null safety is enabled. // Try using a list literal,…
iDecode
  • 22,623
  • 19
  • 99
  • 186
55
votes
8 answers

How to enable Null-Safety in Flutter?

I tried to use null safety, but it's giving me this error: This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.10.0 or higher, and running 'pub get'. I changed my…
Abdallah El-Rashedy
  • 751
  • 1
  • 10
  • 19
43
votes
6 answers

Generator cannot target libraries that have not been migrated to null-safety

This problem occurs when trying the following command: flutter pub run build_runner build --delete-conflicting-outputs The error message: [SEVERE] json_serializable:json_serializable on lib/models/shipping_address/shipping_address.dart: Generator…
Omar Fayad
  • 1,733
  • 3
  • 11
  • 27
35
votes
10 answers

The library 'package:flutter/material.dart' is legacy, and should not be imported into a null safe library

I have a null-safe library and in the example folder I'm using the following import: import 'package:flutter/material.dart'; However, the linter is giving me the following warning: The library 'package:flutter/material.dart' is legacy, and should…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
33
votes
5 answers

After migrating flutter code to null-safety, mock objects not accepting `any`

After the release of Flutter 2, I've migrated my code to sdk: '>=2.12.0 <3.0.0' and all codes are "sound null safety" now. But I encountered errors in unit tests with mockito…
Arash
  • 825
  • 2
  • 11
  • 19
30
votes
9 answers

Dart: Custom "copyWith" method with nullable properties

I'm trying to create a "copyWith" method for my class, and it works with most scenarios. The problem is when I try to set a nullable property to null, because my function cannot recognize whether it's intentional or not. Ex.: class Person { final…
Gustavo Ifanger
  • 403
  • 4
  • 6
29
votes
2 answers

How is Dart "sound null-safety" different from Kotlin null safety?

This Dart official 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…
28
votes
6 answers

Flutter required keyword

I don't really understand how required works. For example I've seen this code: class Test{ final String x; Test({ required this.x }); factory Test.initial(){ return Test(x: ""); } } But what should required do here? Seems like it…
Little Monkey
  • 5,395
  • 14
  • 45
  • 83
25
votes
4 answers

The method '[]' can't be unconditionally invoked because the receiver can be 'null'

I'm new to Flutter. I am trying to develop an application. I want to show the staff list in the Firebase database. However, I am getting the following error. Error : The method '[]' can't be unconditionally invoked because the receiver can be…
1
2 3
78 79