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

Ideally you’d start with a List<String> in the first place. If you’re building your list like: String? s = maybeNullString(); var list = <String?>[ ‘foo’, ‘bar’, someCondition ? ‘baz’ : null, s, ]; then you instead can use collection-if to avoid inserting null elements: String? s = maybeNullString(); var list = <String?>[ ‘foo’, ‘bar’, if … Read more

Non-nullable instance field must be initialized

(Your code was fine before Dart 2.12, null safety) With null safety, Dart doesn’t know if you actually assigned a value to count. Dart initializes objects in two phases, and Dart expects all member variables to already be initialized when the constructor’s body executes. Because your members are non-nullable and haven’t been initialized to non-null … Read more

The parameter can’t have a value of ‘null’ because of its type in Dart

Why The reason this happens is because with null safety enabled, your non-nullable parameter factor or key cannot be null. In the function and the constructor, these values might be null when the function is called without the named parameter: calculate() or Foo(). However because the types (int and Key) are non-nullable, this is invalid … Read more

The argument type ‘Function’ can’t be assigned to the parameter type ‘void Function()?’ after null safety

Change your code to accept a VoidCallback instead of Function for the onPressed. By the way VoidCallback is just shorthand for void Function() so you could also define it as final void Function() onPressed; Updated code: class DrawerItem extends StatelessWidget { final String text; final VoidCallback onPressed; const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key); … Read more

Cannot run with sound null safety because dependencies don’t support null safety

First, you should read through the guide to understand unsound null safety. If you are sure that you want to run your application with unsound null safety, you can use the following command: flutter run –no-sound-null-safety The –no-sound-null-safety option is not documented in the article, however, I have not experienced any problems with it for … Read more

What is Null Safety in Dart?

1. Null safety / non-nullable (by default) The null safety / non-nullable (by default), short NNBD, feature can currently be found at nullsafety.dartpad.dev. Keep in mind that you can read the full spec here and full roadmap here. Now, sound null safety has also been officially announced for Dart. 2.1. What does non-nullable by default … Read more