解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null

简介: 解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null

出现场景

Flutter SDK升级到3.0,运行时报以下警告。

虽然不影响程序的运行,但是看着很烦。

lib/stress_test/stress_test_page.dart:120:22: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../develop_env/flutter_3.0/packages/flutter/lib/src/widgets/binding.dart').
      WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
                     ^

解决方案

这是因为在Flutter 3.0中,binding的instance是不可为空的,所以不需要使用!

下面有2种情况。

三方依赖库

如果是依赖的库要使用到了Binding.instance,去pub上看看库的新版本有没有兼容3.0。如果有就升级库的版本。

比如我的项目用到了getx 4.6.1,是Flutter 3.0出来之前的版本。

../../develop_env/flutter_3.0/.pub-cache/hosted/pub.flutter-io.cn/get-4.6.1/lib/get_state_manager/src/simple/get_controllers.dart:96:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../develop_env/flutter_3.0/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.removeObserver(this);
                   ^

去pub上查看更新记录(changelog),可以看到4.6.2兼容了Flutter 3.0。

[4.6.5] #
Fix pub dev score

[4.6.4] 
Added backward compatibility with flutter 2.

[4.6.3] 
Fix SDK constraints

[4.6.2] 
Added compatibility with flutter 3.0

[4.6.1] 
Fix GetConnect on Flutter web

所以我们只需要将get的版本更改为4.6.2或以上即可。

dependencies:
  # get: ^4.6.1
  get: ^4.6.2

本地代码

如果是项目中有用到Binding.instance,可以使用dart命令dart fix --apply自动修复,这样就会自动把instance后面的!去掉。

adodeMacBook-Pro:fusion_pro wangyang$ dart fix --apply
Computing fixes in fusion_pro... 105.4s
Applying fixes...                      0.0s

lib/pages/splash_page.dart
  UNNECESSARY_NON_NULL_ASSERTION • 1 fix

lib/stress_test/stress_test_page.dart
  UNNECESSARY_NON_NULL_ASSERTION • 1 fix

2 fixes made in 2 files.

相关文章
|
4月前
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast
95 1
|
4月前
|
Dart JavaScript 前端开发
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
159 4
|
4月前
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
51 4
|
4月前
|
API
Flutter 解决Type ‘MouseCursor‘ not found.
Flutter 解决Type ‘MouseCursor‘ not found.
47 0
|
7月前
|
Dart JavaScript 安全
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
|
Dart 安全
Flutter开发Cannot run with sound null safety报错
Flutter开发Cannot run with sound null safety报错
|
Dart 开发工具 Kotlin
【错误记录】Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )
【错误记录】Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )
670 0
【错误记录】Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )
|
存储
存储过程中“ 警告: 聚合或其他 SET 操作消除了 Null 值” 导致错误的解决
在set nocount on 后 添加 SET ANSI_WARNINGS OFF  设置关闭WARNINGS
1824 0
|
2月前
|
Android开发 iOS开发 容器
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
下一篇
DataWorks