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
12
votes
2 answers

Mockito - stub a method after the null-safety migration

Before the null safety I could simply mock up the sendRequest(...) method like that: void stubBaseRepositorySendRequestResponse(String response) { when(baseRepository.sendRequest(onGetData: anyNamed('onGetData'))) .thenAnswer((_) { …
Pablito
  • 521
  • 4
  • 15
12
votes
1 answer

How to increment/decrement a nullable expression in Dart's Sound Null Safety: `!++`?

I am using the Sound Null Safety in Dart, and i have the following code int? _counter; void _incrementCounter() { setState(() { if (_counter!=null) _counter++; }); } Now, since _counter is not a local variable, it can not be promoted…
deczaloth
  • 7,094
  • 4
  • 24
  • 59
12
votes
3 answers

I migrated to null safety and i cant run "flutter pub run build_runner build" without an error being thrown

I have added // @dart=2.9 to all my files but the build_runner won't do its thing without throwing this error: Warning: Operand of null-aware operation '?.' has type 'SendPort' which excludes null. - 'SendPort' is from 'dart:isolate'. …
nnamdi
  • 131
  • 1
  • 6
11
votes
3 answers

Null safety migration error: package has unmigrated dependencies. But all my dependencies declare support for null-safety

Im trying to migrate dart null safety but I get the following error when I run dart migrate Bad state: Error: package has unmigrated dependencies. Before migrating your package, we recommend ensuring that every library it imports (either directly…
Dalon
  • 566
  • 8
  • 26
10
votes
2 answers

Why do we need to add late modifier while declaring the variables?

Its a general question. Not coding related. Everywhere I follow flutter tutorials I see the simple variable declarations. But when I write the same code (I think because of updating) it requires to add late modifier. late modifier can be used while…
doc
  • 828
  • 2
  • 6
  • 18
10
votes
2 answers

Flutter: Equatable props getter with optional parameter

I have an object that extends Equatable and contains optional parameters. If I try to add that parameter into the props getter, I get an error The element type 'String?' can't be assigned to the list type 'Object'. However, not adding it would imply…
Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75
10
votes
5 answers

Flutter - 'the property 'settings' can't be unconditionally accessed because the receiver can be 'null''

The property 'settings' can't be unconditionally accessed because the receiver can be 'null', what to do my code : class DressDetailsScreen extends StatelessWidget { static const routeName = '/DressDetailsScreen'; @override Widget…
MhdBasilE
  • 356
  • 1
  • 4
  • 13
10
votes
3 answers

"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()

i'm new at dart. I don't know what kind of mistake i have made, but this code didn't work. Is a simple code, just read the age in terminal and say it's underage or over 18. import 'dart:io'; main(){ print("Entre com a sua idade: "); var input…
Bruno Proença
  • 113
  • 1
  • 4
10
votes
4 answers

Singleton Class in Flutter with NullSafety

I have this class which takes some parameters by using the factory constructor, if instance is null, a new object will be created; if it's not null, the value of instance will be returned so we always receive the same object all the time…
Morez
  • 2,085
  • 2
  • 10
  • 33
9
votes
2 answers

Null safety type promotion when assigning non-null value literal

In nullsafety.dartpad.dev if I write the following code: void main() { String? name = 'Bob'; print(name.length); } I get the following compile-time error: An expression whose value can be 'null' must be null-checked before it can be…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
8
votes
2 answers

Flutter WillPopScope with AlertDialog migration to null-safety

I have recently migrated my Flutter app to null-safety but WillPopScope in combination with AlertDialog causes a problem. WillPopScope expects Future but showDialog returns Future and I can't figure out how to cast one onto the…
duffy
  • 615
  • 1
  • 9
  • 25
8
votes
5 answers

Can't load Kernel binary: Invalid SDK hash in Flutter

Whenever I try to run a dart tool like Dart Migrate I get the following error and I am unable to run that tool. Is there a way to solve this problem or I have to reinstall Flutter? Btw every thing is okay with Flutter Doctor
Junaid Hassan Final
  • 307
  • 1
  • 2
  • 11
8
votes
2 answers

Flutter 2.0.2 Null check operator used on a null value

While I know this may look like a duplicate with how many different posts there are about this error but I am getting this error for (which I assume) are different reasons on four different screens. My app launches but I end up getting the Red…
Amxela
  • 606
  • 2
  • 6
  • 21
8
votes
2 answers

Problem buliding old project with flutter 2.0.0

After upgrading to flutter 2.0.0 I can't build the app anymore. this is the output. My problem is I have heavy usage of libraries and a lot of them are not null safe and I can't upgrade all of my dependencies to null safe mode. Does anyone have any…
Amir Ebrahimi
  • 547
  • 1
  • 7
  • 22
8
votes
5 answers

Best approach to null safe AppLocalization strings

Question I am using AppLocalizations.of(context).myString to internationalize strings in my null safe flutter app. My IDE tells me that AppLocalizations.of(context) can return null. What's the best approach to handle this? Is there a way to ensure…
Adam Griffiths
  • 680
  • 6
  • 26
  • 60
1 2
3
78 79