flutter 2.0问世已久,3.3去年已经发布了Flutter 3.3 现已发布。能否直接升级呢?做过Android开发的同学都知道,跨越的版本中会肯定有遗漏的地方需要适配。 如果sdk设置2.10.0是可以正常运行的,不需要做空安全适配
environment:
sdk: '>=2.12.0 <3.0.0'
- 什么是null safety?
如果name就可以为空怎么处理呢?我们可以给可以为空的类型后面加上?
void main(){
String? name;
print('name is $name.');
}
List中的String如果非要为空,则需要这样写:
List<String?> aList = ['one', null, 'three'];
对于list本身来说,它也是不能为空的,如果要为空,则需要这样写:
List<String>? aStrings;
!操作符 如果某个对象确定不是null,那么可以在表达式后面添加!,如下所示:
String? word;
word = aListOfNullableStrings.first;
print(word!.length);
late关键字 有时候,我们知道某个对象一定不是空,但是目前来说,并不能立刻对其进行赋值,这时候就需要使用到late关键字。
- api变化
首先最多的应该是 List 对象的修改,因为 List的构造函数已经被 Deprecated.
The default 'List' constructor isn't available when null safety is enabled. (Documentation) Try using a list literal, 'List.filled' or 'List.generate'.
,所以你需要使用 [] 来替换你原有的实现,“体力活”了。
1、Scaffold 的 resizeToAvoidBottomPadding 需要使用 resizeToAvoidBottomInset 参数替代。
2、Theme.of(context, shadowThemeOnly: true) 的 shadowThemeOnly 参数正式取消。
3、新增的DateUtils 到 'package:flutter/material.dart' 里,可能会与你的项目里的 DateUtils 命名冲突。
其他问题
1、包名替换
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
2、api过期的修改
在这里插入代码片
iosOption.setDesiredAccuracy("kCLLocationAccuracyBest"); // 设置预期精度参数
pr = new ProgressDialog(context,
type: ProgressDialogType.Download,
isDismissible: true,
showLogs: true);
if (!pr.isShowing()) {
pr.show();
}
- 依赖仓库升级
如图所示,正常支持 null-safety 的包在 pub 上是有 Null safety 的标签,这时候只要修改你的依赖版本,使用支持空声明安全的插件版本就可以了。
因为大量的插件升级就可能带来版本冲突,比如 permission 版本冲突,在 json_serializable 和 built_value_generator 中他们分别依赖了不同的 analyzer 版本,所以会有版本冲突问题。
部分插件还不支持,只能替换或者自己开发了
- 针对空声明安全的调整
自动迁移null
dart migrate --apply-changes --skip-import-check
自动生成的代码举例,更多的是?以及!
- 奇葩问题
在这里插入代码片
* What went wrong:
Execution failed for task ':app:stripDebugDebugSymbols'.
> No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 20s
Exception: Gradle task assembleDebug failed with exit code 1
APP build文件设置ndk版本 25.2.9519653
2、 终于可以正常运行了,竟然红屏。定位不到具体的页面代码行数
挨个删除页面代码定义的变量,最终 vsync: ScrollableState(),
修改为下方代码
TabController(
length: 4,
vsync: this,
)
下一步计划迁移2.10.0 😂,3.0……