首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新的颤振更新强制空安全?

新的颤振更新强制空安全?
EN

Stack Overflow用户
提问于 2020-12-07 23:01:30
回答 3查看 12.1K关注 0票数 5

我刚刚更新到flutter版本1.25.0-5.0.pre.92,我在我的代码中得到了许多空安全相关的错误,以前工作得很好。我没有以任何方式选择零安全,我所做的一切都是运行flutter升级,现在它假设我想使用零安全。

以下是错误:

代码语言:javascript
复制
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.

有没有办法从我的代码中删除空的安全检查,或者现在强制我做迁移?(这是大量的工作)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-13 17:47:22

我设法让我的项目通过以下方式工作:

运行flutter clean

  • Running flutter create .

的pub spec.yaml

  • Restarting IDE

  • 中的
  1. Setting sdk: ">=2.7.0 <2.12.0"

这对我来说似乎是一个错误,因为如果你再次运行flutter clean,整个项目就会回到使用空安全的状态。

此外,我正在使用Flutter的一些最新功能,但如果你不需要它们,你可以直接切换到稳定频道,它还没有这些变化。

票数 6
EN

Stack Overflow用户

发布于 2020-12-08 01:09:32

更改"environment sdk“版本,并设置一个下限。详情请访问:https://dart.dev/null-safety#enable-null-safety

票数 1
EN

Stack Overflow用户

发布于 2021-10-26 05:39:57

如果您对重构代码以满足新的Flutter SDK版本感兴趣,可以通过添加?在变量类型之后。

代码语言:javascript
复制
String? name;  // initialized to null by default
int? age = 36;  // initialized to non-null
age = null; // can be re-assigned to null

有关更多信息,请查看此链接的说明:https://codewithandrea.com/videos/dart-null-safety-ultimate-guide-non-nullable-types/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65184126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档