5

I just updated to flutter version 1.25.0-5.0.pre.92 and I'm getting lots of null-safety related errors in my code which was previously working fine. I have not opted into null safety in any way, all I did was run flutter upgrade, and now it assumes I want to use null safety.

These are the errors:

org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:165:81: Error: The parameter 'rowKeys' can't have a value of 'null' because of its type 'List<Key>', but the implicit default value is 'null'.
 - 'List' is from 'dart:core'.
 - 'Key' is from 'package:flutter/src/foundation/key.dart' ('../sdk/flutter/packages/flutter/lib/src/foundation/key.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
  Widget _getCol3(BuildContext context, ScrollController controller, [List<Key> rowKeys]){
                                                                                ^^^^^^^
org-dartlang-app:/change_notifiers/theme_parameters.dart:17:5: Error: The value 'null' can't be assigned to a variable of type 'ThemeData' because 'ThemeData' is not nullable.
 - 'ThemeData' is from 'package:flutter/src/material/theme_data.dart' ('../sdk/flutter/packages/flutter/lib/src/material/theme_data.dart').
    null,
    ^
org-dartlang-app:/change_notifiers/theme_parameters.dart:27:35: Error: Method 'withOpacity' cannot be called on 'Color?' because it is potentially null.
 - 'Color' is from 'dart:ui'.
Try calling using ?. instead.
          color: Colors.grey[700].withOpacity(0.9),
                                  ^^^^^^^^^^^
org-dartlang-app:/change_notifiers/theme_parameters.dart:45:37: Error: Method 'withOpacity' cannot be called on 'Color?' because it is potentially null.
 - 'Color' is from 'dart:ui'.
Try calling using ?. instead.
            color: Colors.grey[700].withOpacity(0.9),
                                    ^^^^^^^^^^^
org-dartlang-app:/pages/heroes/page_heroes.dart:86:42: Error: The value 'null' can't be assigned to a variable of type 'Widget Function(BuildContext, Animation<double>, HeroFlightDirection, BuildContext, BuildContext)' because 'Widget Function(BuildContext, Animation<double>, HeroFlightDirection, BuildContext, BuildContext)' is not nullable.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../sdk/flutter/packages/flutter/lib/src/animation/animation.dart').
 - 'HeroFlightDirection' is from 'package:flutter/src/widgets/heroes.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart').
                        shuttleBuilder = null;
                                         ^
org-dartlang-app:/pages/heroes/page_heroes.dart:88:47: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
                      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                                              ^^^^^^^^^^^^^^^^^^^^
org-dartlang-app:/pages/heroes/page_heroes.dart:103:47: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
                      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                                              ^^^^^^^^^^^^^^^^^^^^
org-dartlang-app:/pages/heroes/page_heroes.dart:116:42: Error: The value 'null' can't be assigned to a variable of type 'Widget Function(BuildContext, Animation<double>, HeroFlightDirection, BuildContext, BuildContext)' because 'Widget Function(BuildContext, Animation<double>, HeroFlightDirection, BuildContext, BuildContext)' is not nullable.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../sdk/flutter/packages/flutter/lib/src/animation/animation.dart').
 - 'HeroFlightDirection' is from 'package:flutter/src/widgets/heroes.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart').
                        shuttleBuilder = null;
                                         ^
org-dartlang-app:/pages/heroes/page_heroes.dart:118:47: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
                      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                                              ^^^^^^^^^^^^^^^^^^^^
org-dartlang-app:/pages/heroes/page_heroes.dart:133:47: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
                      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                                              ^^^^^^^^^^^^^^^^^^^^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:37:5: Error: The value 'null' can't be assigned to a variable of type 'TextEditingController' because 'TextEditingController' is not nullable.
 - 'TextEditingController' is from 'package:flutter/src/widgets/editable_text.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
    null,
    ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:38:5: Error: The value 'null' can't be assigned to a variable of type 'TextEditingController' because 'TextEditingController' is not nullable.
 - 'TextEditingController' is from 'package:flutter/src/widgets/editable_text.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
    null,
    ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:180:35: Error: The getter 'text' isn't defined for the class 'Object?'.
 - 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'text'.
                    value = value.text;
                                  ^^^^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:183:33: Error: The argument type 'Object?' can't be assigned to the parameter type 'String'.
 - 'Object' is from 'dart:core'.
                    return Text(value, style: Theme.of(context).textTheme.headline3,);
                                ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:243:29: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      colBackgroundColors: [null, null, null, Colors.green.withOpacity(0.4), Colors.red.withOpacity(0.4)],
                            ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:243:35: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      colBackgroundColors: [null, null, null, Colors.green.withOpacity(0.4), Colors.red.withOpacity(0.4)],
                                  ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:243:41: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      colBackgroundColors: [null, null, null, Colors.green.withOpacity(0.4), Colors.red.withOpacity(0.4)],
                                        ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:244:29: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      rowBackgroundColors: [null, null, null, null, Colors.indigo.withOpacity(0.4)],
                            ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:244:35: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      rowBackgroundColors: [null, null, null, null, Colors.indigo.withOpacity(0.4)],
                                  ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:244:41: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      rowBackgroundColors: [null, null, null, null, Colors.indigo.withOpacity(0.4)],
                                        ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:244:47: Error: The value 'null' can't be assigned to a variable of type 'Color' because 'Color' is not nullable.
 - 'Color' is from 'dart:ui'.
      rowBackgroundColors: [null, null, null, null, Colors.indigo.withOpacity(0.4)],
                                              ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:246:26: Error: The value 'null' can't be assigned to a variable of type 'TextAlign' because 'TextAlign' is not nullable.
 - 'TextAlign' is from 'dart:ui'.
      columnAlignments: [null, null, null, null, TextAlign.right],
                         ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:246:32: Error: The value 'null' can't be assigned to a variable of type 'TextAlign' because 'TextAlign' is not nullable.
 - 'TextAlign' is from 'dart:ui'.
      columnAlignments: [null, null, null, null, TextAlign.right],
                               ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:246:38: Error: The value 'null' can't be assigned to a variable of type 'TextAlign' because 'TextAlign' is not nullable.
 - 'TextAlign' is from 'dart:ui'.
      columnAlignments: [null, null, null, null, TextAlign.right],
                                     ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:246:44: Error: The value 'null' can't be assigned to a variable of type 'TextAlign' because 'TextAlign' is not nullable.
 - 'TextAlign' is from 'dart:ui'.
      columnAlignments: [null, null, null, null, TextAlign.right],
                                           ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:247:19: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      colStyles: [null, null, null, null, Theme.of(context).textTheme.caption],
                  ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:247:25: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      colStyles: [null, null, null, null, Theme.of(context).textTheme.caption],
                        ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:247:31: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      colStyles: [null, null, null, null, Theme.of(context).textTheme.caption],
                              ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:247:37: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      colStyles: [null, null, null, null, Theme.of(context).textTheme.caption],
                                    ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:247:71: Error: A value of type 'TextStyle?' can't be assigned to a variable of type 'TextStyle' because 'TextStyle?' is nullable and 'TextStyle' isn't.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      colStyles: [null, null, null, null, Theme.of(context).textTheme.caption],
                                                                      ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:248:19: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      rowStyles: [null, null, null, null, Theme.of(context).textTheme.headline6],
                  ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:248:25: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      rowStyles: [null, null, null, null, Theme.of(context).textTheme.headline6],
                        ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:248:31: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      rowStyles: [null, null, null, null, Theme.of(context).textTheme.headline6],
                              ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:248:37: Error: The value 'null' can't be assigned to a variable of type 'TextStyle' because 'TextStyle' is not nullable.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      rowStyles: [null, null, null, null, Theme.of(context).textTheme.headline6],
                                    ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:248:71: Error: A value of type 'TextStyle?' can't be assigned to a variable of type 'TextStyle' because 'TextStyle?' is nullable and 'TextStyle' isn't.
 - 'TextStyle' is from 'package:flutter/src/painting/text_style.dart' ('../sdk/flutter/packages/flutter/lib/src/painting/text_style.dart').
      rowStyles: [null, null, null, null, Theme.of(context).textTheme.headline6],
                                                                      ^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:260:47: Error: The value 'null' can't be assigned to a variable of type 'bool' because 'bool' is not nullable.
          selected[rowsIds.indexOf(row.id)] = null;
                                              ^
/C:/Users/Dayan/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/extended_image-1.1.0/lib/src/extended_image.dart:786:44: Error: No named parameter with the name 'nullOk'.
    _invertColors = MediaQuery.of(context, nullOk: true)?.invertColors ??
                                           ^^^^^^
../sdk/flutter/packages/flutter/lib/src/widgets/media_query.dart:818:25: Context: Found this candidate, but the arguments don't match.
  static MediaQueryData of(BuildContext context) {
                        ^^
/C:/Users/Dayan/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/wave-0.0.8/lib/config.dart:46:13: Error: Method not found: 'throwNullError'.
            throwNullError('custom', 'colors` or `gradients');
            ^^^^^^^^^^^^^^
/C:/Users/Dayan/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/wave-0.0.8/lib/config.dart:60:13: Error: Method not found: 'throwNullError'.
            throwNullError('custom', 'durations');
            ^^^^^^^^^^^^^^
/C:/Users/Dayan/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/wave-0.0.8/lib/config.dart:66:13: Error: Method not found: 'throwNullError'.
            throwNullError('custom', 'heightPercentages');
            ^^^^^^^^^^^^^^
org-dartlang-app:/pages/future_handling/page_future_handling.dart:32:10: Error: Field 'widgetToExport' should be initialized because its type 'Widget' doesn't allow null.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
  Widget widgetToExport;
         ^^^^^^^^^^^^^^
org-dartlang-app:/pages/future_handling/page_future_handling.dart:82:13: Error: Field 'widgetToExportKey' should be initialized because its type 'GlobalKey<State<StatefulWidget>>' doesn't allow null.
 - 'GlobalKey' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'State' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'StatefulWidget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
  GlobalKey widgetToExportKey;
            ^^^^^^^^^^^^^^^^^
org-dartlang-app:/pages/heroes/page_heroes.dart:33:10: Error: Field 'widgetToExport' should be initialized because its type 'Widget' doesn't allow null.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
  Widget widgetToExport;
         ^^^^^^^^^^^^^^
org-dartlang-app:/pages/heroes/page_heroes.dart:34:28: Error: Field 'shuttleBuilder' should be initialized because its type 'Widget Function(BuildContext, Animation<double>, HeroFlightDirection, BuildContext, BuildContext)' doesn't allow null.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'Animation' is from 'package:flutter/src/animation/animation.dart' ('../sdk/flutter/packages/flutter/lib/src/animation/animation.dart').
 - 'HeroFlightDirection' is from 'package:flutter/src/widgets/heroes.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/heroes.dart').
  HeroFlightShuttleBuilder shuttleBuilder;
                           ^^^^^^^^^^^^^^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:30:10: Error: Field 'col1' should be initialized because its type 'Widget' doesn't allow null.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
  Widget col1;
         ^^^^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:31:10: Error: Field 'col2' should be initialized because its type 'Widget' doesn't allow null.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
  Widget col2;
         ^^^^
org-dartlang-app:/pages/lightweight_table/page_lightweight_table.dart:32:10: Error: Field 'col3' should be initialized because its type 'Widget' doesn't allow null.
 - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../sdk/flutter/packages/flutter/lib/src/widgets/framework.dart').
  Widget col3;
         ^^^^
Failed to compile application.

Is there any way to remove the null safety checks from my code or do am I forced to do the migration now? (it's a lot of work).

Akif
  • 7,098
  • 7
  • 27
  • 53
Wish Master
  • 71
  • 1
  • 1
  • 7

3 Answers3

6

I managed to get my project working by:

  1. Setting sdk: ">=2.7.0 <2.12.0" in pub spec.yaml
  2. Restarting IDE
  3. Running flutter clean
  4. Running flutter create .

This seems to be a bug for me because, if you then run flutter clean again, the whole project goes back to using null-safety.

Also, I am using some of the newest features of Flutter, but if you don't need them you could just switch to the stable channel which does not have those changes yet.

  • Thanks, I do need the latest features, like desktop suport... I decided to just migrate to null safety, I wanted to do it evetually anyways – Wish Master Dec 14 '20 at 11:32
1

Change the "environment sdk" version with a lower bound. Details are at https://dart.dev/null-safety#enable-null-safety

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
1

If you are interested in refactoring your code to meet the new Flutter SDK version, you can make the variables nullable by adding a ? after the variable type.

String? name;  // initialized to null by default
int? age = 36;  // initialized to non-null
age = null; // can be re-assigned to null

For more info, look at the explanation at this link: https://codewithandrea.com/videos/dart-null-safety-ultimate-guide-non-nullable-types/

Winnifred
  • 1,202
  • 1
  • 8
  • 10