解决升级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.

目录
打赏
0
1
1
0
54
分享
相关文章
|
11月前
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
259 1
|
11月前
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
177 0
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
366 4
|
11月前
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
99 4
|
11月前
|
API
Flutter 解决Type ‘MouseCursor‘ not found.
Flutter 解决Type ‘MouseCursor‘ not found.
96 0
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
error • The parameter ‘name‘ can‘t have a value of ‘null‘ because of its type错误
error • The parameter ‘name‘ can‘t have a value of ‘null‘ because of its type错误
【错误记录】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 )
753 0
【错误记录】Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问