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

Dart null safety - How to disable null safety analysis for certain file?

The problem: some of pub packages doesn't support null safety yet. For example intl_translation cannot parse a file with null safety operators and throw Errors as below if any of symbols (as ?, ! etc) found. Invalid argument(s): Parsing errors in…
Arenukvern
  • 438
  • 6
  • 11
6
votes
3 answers

Passing a function as parameter in flutter with null safety

After upgrading to Flutter 1.25.0-8.1.pre null safety is enabled by default and I started to modify the code of my project. Everything is working fine except functions passed as parameters like in the following example: class AppBarIcon extends…
Felix Mittermeier
  • 183
  • 1
  • 2
  • 11
6
votes
1 answer

How to build Null Factory Constructors with Null Safety?

A common Dart pattern before null safety for creating static method holder classes was the following: class MyMethodScope { /// Prevents instantiation of this class. factory MyMethodScope._() => null; static void noop() {} } This is not…
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
5
votes
2 answers

CameraUtils.java:154: warning: [deprecation] get(int,int) in CamcorderProfile has been deprecated

I upgraded the old project and tried to migrate it to the null-safety I am getting this error can't get any answers regarding that any help
5
votes
3 answers

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

I have: Future foo() async => true; This is allowed: Future bar() { return foo(); // Works } but this is not: Future baz() async { return foo(); // Error } In both bar and baz I'm returning a Future, but why first…
iDecode
  • 22,623
  • 19
  • 99
  • 186
5
votes
3 answers

Why is key required in constructor?

I have created class which extend StatefulWidget class RegistrationPage extends StatefulWidget { final String email; const RegistrationPage({Key key, required this.email}) : super(key: key); @override _RegistrationPage createState() =>…
traki111
  • 361
  • 1
  • 3
  • 13
5
votes
4 answers

Flutter default value for a Function

I am currently learning a course on Flutter. They provide you with an old stub project (nullable). When I try to migrate to Flutter 2.12.0 or higher - null safety kicks in. I have a basic understanding on how it works - but this I cannot find…
5
votes
4 answers

Generate App Bundle with --no-sound-null-safety Flutter

I am trying to generate the App Bundle via Android studio (Build -> Generate Signed Bundle / APK), and I am running into an error when building a bundle. Error: Cannot run with sound null safety, because the following dependencies don't support null…
mrgnhnt96
  • 3,562
  • 1
  • 17
  • 36
5
votes
3 answers

Null safety and Maps

How do you handle the following case: void main() { print(getInt('d')); } Map myMap = {'a':1, 'b':2, 'c':3}; int getInt(String key) { if (!myMap.containsKey(key)) { myMap[key] = 0; } return myMap[key]!; } myMap[key] is…
Thierry
  • 7,775
  • 2
  • 15
  • 33
4
votes
2 answers

Accessing nested property in AsyncSnapshot

So, I am learning to use the FutureBuilder class in Flutter. My code works, but I wonder if I could improve the way I access the data from the snapshot. My FutureBuilder is like that: @override Widget build(BuildContext context) { return…
user20874439
4
votes
2 answers

How can I make null-safety assertions to avoid using null check (!) or conditional (?) operators in Flutter?

Dart compiler does not understand that the variable can not be null when I use it inside an if (x != null) statement. It still requires to use conditional ? or null check ! operators to access one of the variable's fields or methods. Here is an…
rasitayaz
  • 400
  • 2
  • 16
4
votes
1 answer

Null check operator used on a null value - Flutter

I was trying to make a google drive app that lists files from the drive. but I got Null check operator used on a null value error. I got it what's happening. but I could not solve it. @override Widget build(BuildContext context) { return…
Mahi
  • 1,297
  • 1
  • 14
  • 28
4
votes
1 answer

Why do I need to return null for functions with nullable return type?

I get a warning in this code: Future foo() async { if (someCondition) return 42; } This function has a nullable return type of 'FutureOr', but ends without returning a value. I'm telling Dart that the Future may complete with a…
iDecode
  • 22,623
  • 19
  • 99
  • 186
4
votes
1 answer

BoxPainter createBoxPainter([onChanged]) => The parameter onChanged can't have a value of null

I've copied/pasted some sample code for creating a custom BoxDecoration: class FrameDecoration extends Decoration { @override BoxPainter createBoxPainter([onChanged]) { return _CustomDecorationPainter(); } I get this error: The…
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
4
votes
5 answers

The name 'MyApp' isn't a class. Try correcting the name to match an existing class. flutter

I don't know what is the problem is / how to fix this while I tring to code it shows this error on the test folder widget_test.dart x testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a…
Leskeboy
  • 107
  • 1
  • 1
  • 9