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
23
votes
5 answers

Equivalent of if let and guard let of Swift in Dart

Just started Flutter with native iOS background, so I just have a quick question about Dart beta null safety. So in Swift, because they have the idea of null safety from the beginning just like Kotlin, there are 2 features that I really like about…
Son Nguyen
  • 1,124
  • 2
  • 10
  • 24
22
votes
3 answers

A nullable expression can't be used as a condition

I am getting a "nullable expression" error. The problem is occurring in the following lines: left: isSideBarOpenedAsync.data ? 0 : 0, right: isSideBarOpenedAsync.data ? 0 : screenWidth - 35, Where isSideBarOpenedAsync is type…
Tμrf
  • 404
  • 1
  • 3
  • 14
22
votes
2 answers

Error: A library can't opt out of null safety by default, when using sound null safety

When upgrading a Flutter package for null safety I got this error when running flutter test: Error: A library can't opt out of null safety by default, when using sound null safety. // @dart = 2.8 ^^^^^^^^^^^^^^ Failed to load…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
19
votes
3 answers

"Use key in widget constructors" warning each time I create a new widget

I've started getting a warning every time I create a new class. Use key in widget constructors. Prefer const with constant constructor. and so on. Where these errors are coming from and how do I get rid of it?
iDecode
  • 22,623
  • 19
  • 99
  • 186
19
votes
2 answers

How to convert a List to List in null safe Dart?

I have a dart list: List vals; I want to remove any null values and convert it to a List. I've tried: List removeNulls(List list) { return list.where((c) => c != null).toList() as List; } At run time I'm…
Brett Sutton
  • 3,900
  • 2
  • 28
  • 53
18
votes
2 answers

Flutter Error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type

I'm using the new dart version <2.13.0-107.0.dev> with null safety enabled. With this List of tasks: List tasks = [ Task(name: 'find a way to live happy'), Task(name: 'Listen to music!!!'), Task(name: 'Live in the other world…
MadMax
  • 483
  • 1
  • 4
  • 12
16
votes
4 answers

Dart null safety doesn't work with class fields

I have migrated my Dart code to NNBD / Null Safety. Some of it looks like this: class Foo { String? _a; void foo() { if (_a != null) { _a += 'a'; } } } class Bar { Bar() { _a = 'a'; } String _a; } This causes two…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
15
votes
3 answers

What is meant by 'Sort pub dependencies'?

I am trying to migrate a project into null safety mode,after migrating and clearing all the errors, I am getting this problem named 'Sort pub dependencies.dart(sort_pub_dependencies)'. I searched Google but couldn't find any solution. Any help would…
15
votes
1 answer

How to fix "The operator ‘{0}’ can’t be unconditionally invoked because the receiver can be ‘null’"

While trying Dart's Sound Null Safety i came up with a problem: Some Context Creating a new Flutter project i found the following (and very familiar) piece code int _counter = 0; void _incrementCounter() { setState(() { // This call to…
deczaloth
  • 7,094
  • 4
  • 24
  • 59
14
votes
3 answers

Flutter Null Safety Migration says `Package doesn't exist`

Flutter Null Safety Migration I am trying to migrate my project to Flutter Null Safety Version and I am following this official migration guide. When I run the second command it says Package doesn't exist (the Flutter SDK is not available). Command…
Abhishek Kumar
  • 738
  • 1
  • 9
  • 17
14
votes
4 answers

Flutter null safety - The argument type 'Color?' can't be assigned to the parameter type 'Color'

I changed my SDK version for flutter to min , so that I can fix my code for null safety. There is one issue that I don't understand, this line produces the following error: The argument type 'Color?' can't be assigned to the parameter type…
AJ-
  • 1,638
  • 1
  • 24
  • 49
14
votes
4 answers

The property 'docs' cannot be unconditionally accessed because received can be 'null' Flutter

After migrate to null-safety showing this error. What should I do now? Widget chatMessages() { return StreamBuilder( stream: messageStream, builder: (context, snapshot) { return snapshot.hasData ?…
monzim
  • 526
  • 2
  • 4
  • 13
14
votes
5 answers

Dart null safety - retrieving value from map in a null safe way

I had code similar to this example code (never mind that it makes no sense): void foo(Map myMap) { String s = myMap[1]; } The dart analyzer warns me about the line String s = myMap[1]; with the following warning: A value of type…
SebastianJL
  • 169
  • 1
  • 1
  • 8
13
votes
1 answer

What's the differences between "!" and "?" operators in dart null safety?

Could anyone explain a little bit about the differences between using this: final block = blocks?.first; and this: final block = blocks!.first; where blocks is: List? blocks
devmuaz
  • 519
  • 7
  • 18
12
votes
2 answers

Understanding " ! " Bang Operator in dart

I declared a callback method inside a Stateful class like: final void Function(int index)? onSelected; MyBottomNavigationBar({@required this.onSelected}); and called using widget.onselected inside state class…
Ashish Acharya
  • 193
  • 2
  • 7
1
2
3
78 79