gpt4 book ai didi

flutter - 新的 flutter 更新强制无效安全?

转载 作者:行者123 更新时间:2023-12-04 12:20:32 38 4
gpt4 key购买 nike

我刚刚更新到 flutter 版本 1.25.0-5.0.pre.92,我的代码中出现了很多与空安全相关的错误,这些错误以前运行良好。我没有以任何方式选择空安全,我所做的只是运行 flutter 升级,现在它假设我想使用空安全。
这些是错误:

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.

有没有办法从我的代码中删除空安全检查,或者我现在是否被迫进行迁移? (这是很多工作)。

最佳答案

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

  • 设置 sdk: ">=2.7.0 <2.12.0"在 pub spec.yaml 中
  • 重启IDE
  • 运行 flutter clean
  • 运行 flutter create .

  • 这对我来说似乎是一个错误,因为如果你然后运行 ​​ flutter clean再次,整个项目回到使用空安全。
    此外,我正在使用 Flutter 的一些最新功能,但是如果您不需要它们,您可以切换到尚未进行这些更改的稳定 channel 。

    关于flutter - 新的 flutter 更新强制无效安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65184126/

    38 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com