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
3
votes
1 answer

Why am I getting "Null check operator used on a null value" from using rootBundle.load during a Flutter test?

I searched for quite a while and didn't find a clear solution to this issue on SO or other sites. I have a Flutter test: test('Create Repo and Read JSON', () { Repository repository = CreateRepository(); ... } CreateRepository() eventually…
Jeff Neet
  • 734
  • 1
  • 8
  • 22
3
votes
1 answer

The argument type 'void Function(Map)' can't be assigned to the parameter type 'Map'

I'm trying to send the filter to the filter screen but getting an error. The argument type 'void Function(Map)' can't be assigned to the parameter type 'Map'. Main screen code import…
Syed Rehan
  • 651
  • 1
  • 3
  • 11
3
votes
1 answer

null safety issue for getter with if-statment

I'm trying to retrive User data from sharedPreferances with code: User? get user { String? u = sharedPreferences.getString("user"); if (u == null) { return null; } else { return User.fromJson(jsonDecode(u)); } } but…
pb4now
  • 1,305
  • 4
  • 18
  • 45
3
votes
1 answer

Proper using of question mark (null safety) in flutter

Is it equal in flutter null safety? if(widget.mapPickerController != null){ widget.mapPickerController!.mapMoving = mapMoving; widget.mapPickerController!.mapFinishedMoving = mapFinishedMoving; } …
Akbar Pulatov
  • 2,955
  • 2
  • 16
  • 33
3
votes
3 answers

Constructor doesn't satisfy null safety in Dart

Just getting started on Dart. DartPad says my code is not null-safe, but I don't see a way to initialize a "Point" without assigning a value to x and y, so it should be null-safe, shouldn't it? void main(){ Point p = new Point(1.0,2.0); …
Sir Falk
  • 113
  • 5
3
votes
2 answers

Why can't non-nullable fields be initialized in a constructor body in Dart?

Often times I have an instance field that needs to be initialized in a constructor. For example, it might be needed to be calculated based on other instance fields, hence I can't initialize it inline (where declared) or with a constructor…
Titan
  • 2,875
  • 5
  • 23
  • 34
3
votes
1 answer

Dart null safety: the operand cannot be null, so the condition is always true

I am trying to double-check if the User object is successfully created, but Null saftey says the operand cannot be null, so the condition is always true What if in a scenario where the json data contains invalid type, in this case there might be…
Manas
  • 3,060
  • 4
  • 27
  • 55
3
votes
1 answer

null safety with flutter () => Null is not a sub type '(() => Map)? of 'orElse'

type '() => Null' is not a subtype of type '(() => Map)?' of 'orElse' How to get out from this error? and this is the my code snippet as below. FutureBuilder( future:…
3
votes
2 answers

The return type 'Future Function()' isn't a 'Future', as required by the closure's context

I have upgraded my project sdk to >2.12 so I am making the changes and I am stuck at this particular error and not able to find any solution for it. I have made changes in the method as you can see below while calling it I am not sure how to fix…
Lav Sharma
  • 327
  • 1
  • 5
  • 23
3
votes
1 answer

Should we source control the .mocks.dart files created by build_runner for mockito

Because of null safety in dart, we had to update our codes test. We are using mockito for mocking the dependecies for the given class. As per their docs, we decided to use the build_runner method to generate Mock Classes of the dependencies. This is…
ParasGarg
  • 103
  • 1
  • 2
  • 10
3
votes
2 answers

Dart/Flutter widget with optional parameters but at least one required

I'm trying to create a Flutter widget that can be initialized by various parameters, something like this class MyWidget extends StatefulWidget { final int? id; final String? username; MyWidget({this.id, this.username}); @override …
Razvan Fulea
  • 437
  • 4
  • 13
3
votes
0 answers

Flutter path_provider getApplicationSupportDirectory() throws _CastError Null check operator on a null value

I am having issues running my flutter application. I am using objectbox-dart for storing data in my app. So far so good, but when I create a store using path_provider, the getApplicationSupportDirectory() throws _CastError: Null check operator on a…
Shoaib A
  • 63
  • 7
3
votes
1 answer

retrofit_generator:retrofit crash after upgrade to Null Safe version on Flutter

I am trying to upgrade my Flutter app to be Null Safe and I encountered a problem with the retrofit code generator. So I have a RestAPI abstract class declared like this: @RestApi(baseUrl: ApiConsts.authBaseURL) abstract class IAuthApi { factory…
3
votes
1 answer

A value of type 'Future' can't be returned from the method because it has a return type of 'Future

Future foo(BuildContext context) { return Navigator.pushNamed(context, '/bar'); // Error } Error: A value of type 'Future' can't be returned from the function 'foo' because it has a return type of 'Future'
iDecode
  • 22,623
  • 19
  • 99
  • 186
3
votes
1 answer

Flutter/Dart Null Safety on _formKey.currentState.validate()?

I've just updated my minimum sdk in pubspec.yaml; sdk: ">=2.12.0 <3.0.0" Now I have to change all my variables by declaring ? on all Strings and integers. But what shall I do with the formkey? if (_formKey.currentState.validate()) Here's what…
Meggy
  • 1,491
  • 3
  • 28
  • 63