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
8
votes
3 answers

Can late and final be used together?

I am trying NNBD as of now and I would like to know if you can use the new keyword late and final together. From what I understood, a late property can be set anywhere. You are basically telling the analyzer that it will not be null when used. I…
Jonathan
  • 4,724
  • 7
  • 45
  • 65
7
votes
4 answers

Crash when compiling null, at character offset null - Flutter and Dart

I'm new to flutter and dart and I am trying to make a clone of instagram for studying purposes, but I am getting the error below. Could you help me identify where the code is incorrect? The output message error is in the end. I tried google, and…
KaioMartins
  • 115
  • 1
  • 9
7
votes
4 answers

How to implement a singleton with async initialisation and null safety in dart?

I need a Singleton for the shared preferences which has async initialisation but also works with null safety. Usually I used the following singleton implementation, but what is the best way to have a singleton which works with null safety? class…
midi
  • 3,128
  • 5
  • 30
  • 47
7
votes
1 answer

dart migrate gives error in my own code files

When I run "dart migrate" for enabling null-safety, it gives me following output. Before migrating your package, we recommend ensuring that every library it imports (either directly or indirectly) has been migrated to null safety, so that you will…
7
votes
2 answers

Named Constructor with required parameters and null safety

The following code does not compile with sound null safety because it is possible to pass null to the constructor which initializes a non-nullable field myString. class MyClass { String myString; MyClass({@required this.myString}); } I would…
Bobin
  • 278
  • 5
  • 15
7
votes
2 answers

Flutter: json_serializable ignore nullable fields instead of throwing an error

Suppose there are two models User and City @JsonSerializable() class User { int id; String name; City? city; List>? listMapCity; } @JsonSerializable() class City { int id; String name; } Now suppose during…
Himanshu Arora
  • 309
  • 4
  • 13
7
votes
6 answers

How to Resolve The Argument type 'String?' can't be assign to parameter type 'String'

In dart sdk greater than 2.12.0 we are often using '?' to make sure that argument can have null values also, but how to convert datatype with ? to datatype without ? or vice versa. what can be the most preferred way to sort out 'int?' can't of type…
Rishabh Bhardwaj
  • 365
  • 1
  • 5
  • 11
7
votes
3 answers

How to fix 'Text is null' in flutter

I want to create an app which has a TabBarView with two tabs. On the first Tab there is a Textfield and on the other tab there is a text widget which should display the text which you entered into Textfield but I always get an error because text is…
Cl_Rtg08
  • 81
  • 1
  • 1
  • 6
6
votes
3 answers

How to solve error "type 'Null' is not a subtype of type 'String' in type cast"

I've been trying to debug this error type 'Null' is not a subtype of type 'String' in type cast but could not find the the exact place where the error is being produced besides that it is genereated when trigger a POST API call. Shop Class import…
TEZZ
  • 179
  • 1
  • 15
6
votes
2 answers

Null-aware .firstWhere, .singleWhere, .lastWhere in Dart's List?

I often use .firstWhere((E element) -> bool) -> E in my project. When porting it to support null safety I couldn't cleanly handle a scenario when an element is not found in a List instance. .firstWhere, .singleWhere and .lastWhere returns E, not E?…
Marcin Wróblewski
  • 811
  • 1
  • 10
  • 25
6
votes
1 answer

Flutter localization in `Drawer` leads to `Null check operator used on a null value` error

For my debut with Dart/Flutter I created a very simple demo app with a localized text within a Drawer. The app compiles and starts in the Android Emulator, but only to abort shortly after with the error message Null check operator used on a null…
6
votes
1 answer

The current Dart SDK version does not satisfy null-safety dependences

I'm upgrading my project into sound null-safety. After changing the version to environment: sdk: ">=2.12.0 <3.0.0" then done converting my source code into null-safety syntax. When I ran my project, it ran flutter pub get, and the problem…
apieceofcode1801
  • 171
  • 1
  • 1
  • 9
6
votes
3 answers

Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on 'GetIt.call'. After migrating to Null Safety

I'm in the process of migrating over a large project to null safety and I'm coming across a strange error I'm not entirely sure how to fix. "Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on…
Swisscheese
  • 571
  • 1
  • 5
  • 21
6
votes
2 answers

Run android project from a Flutter project with unsound null safety

After sound null safety has arrived, projects that still have dependencies that have not upgraded to null safety yet, can still be run using --no-sound-null-safety flag through the Flutter command. But now I am trying to run the app through the…
6
votes
1 answer

Flutter 2: problem in Intl null safety library

I upgrade my flutter sdk to flutter 2.0.0. After updating sdk I have changed almost all libraries to null safety version: environment: sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter flutter_localizations: sdk: flutter …
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149