flutter sdk 3.13.0 对外发布了,我也改适配下 woo 课程代码。

flutter sdk 3.10.6 woo 课程代码适配说明

woo

视频

https://youtu.be/Cbt7m22W4fE

前言

woowoo-dark

flutter sdk 3.13.0 对外发布了,我也改适配下 woo 课程代码。

一般我都是等版本稳定了再升级。

很多同学问我怎么报错了,内存溢出了,一般我都是看一眼代码没问题,再问问什么 sdk 版本。

大多数同学都是 3.13.x, 3.10.0~3 这种刚冒头的版本。

以下是我升级代码的过程,大家做个参考。

参考

https://docs.flutter.dev/release/archive?tab=macos

步骤

第一步:修改 sdk 版本依赖

pubspec.yaml

environment:
  sdk: ">=3.0.6 <4.0.0"

限定 Flutter SDK 版本区间 >=3.0.6 <4.0.0

第二步:包升级

执行命令

flutter pub upgrade
  another_xlider 1.1.2 (3.0.1 available)
> badges 3.1.1 (was 3.0.2)
  collection 1.17.1 (1.18.0 available)
> dio 5.3.2 (was 5.0.2)
  dropdown_button2 1.7.2 (2.3.8 available)
> extended_image 8.0.2 (was 7.0.2) (8.1.0 available)
  extended_image_library 3.5.3 (3.6.0 available)
  file 6.1.4 (7.0.0 available)
> flutter_native_splash 2.3.1 (was 2.2.16) (2.3.2 available)
> flutter_screenutil 5.9.0 (was 5.7.0)
> flutter_svg 2.0.7 (was 1.1.4)
> image 4.0.17 (was 3.3.0)
  intl 0.18.0 (0.18.1 available)
  matcher 0.12.15 (0.12.16 available)
  material_color_utilities 0.2.0 (0.8.0 available)
> permission_handler 10.4.3 (was 10.2.0)
  petitparser 5.4.0 (6.0.1 available)
  pinput 2.2.31 (3.0.0 available)
> shared_preferences 2.2.0 (was 2.0.18)
  source_span 1.9.1 (1.10.0 available)
  stack_trace 1.11.0 (1.11.1 available)
  stream_channel 2.1.1 (2.1.2 available)
  test_api 0.5.1 (0.6.1 available)
+ vector_graphics 1.1.7
+ vector_graphics_codec 1.1.7
+ vector_graphics_compiler 1.1.7
> wechat_assets_picker 8.6.3 (was 8.4.0) (8.7.0 available)
> wechat_camera_picker 3.8.0 (was 3.7.0) (4.0.0 available)
  xml 6.3.0 (6.4.2 available)

依据升级建议 修改包版本

dependencies:
  flutter:
    sdk: flutter

  flutter_localizations:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.5

  # getx 框架
  get: 4.6.5
  intl: 0.18.0
  package_info_plus: 4.1.0
  shared_preferences: 2.2.0
  dio: 5.3.2
  flutter_svg: 2.0.7
  badges: 3.1.1
  extended_image: 8.0.2
  flutter_screenutil: 5.9.0
  flutter_native_splash: 2.3.1
  carousel_slider: 4.2.1
  validatorless: 1.2.3
  pinput: 2.2.31
  flutter_easyloading: 3.0.5
  encrypt: 5.0.1
  pull_to_refresh: 2.0.0
  photo_view: 0.14.0
  dropdown_button2: 1.7.2
  another_xlider: 1.1.2
  # picker 选择器
  flutter_picker: 2.1.0
  # 底部弹出
  modal_bottom_sheet: 3.0.0-pre
  # permission 权限
  permission_handler: 10.4.3
  # 媒体选择
  wechat_assets_picker: 8.6.3
  wechat_camera_picker: 3.8.0

拉取新包

flutter pub get
Resolving dependencies...
  another_xlider 1.1.2 (3.0.1 available)
  collection 1.17.1 (1.18.0 available)
  dropdown_button2 1.7.2 (2.3.8 available)
  extended_image 8.0.2 (8.1.0 available)
  extended_image_library 3.5.3 (3.6.0 available)
  file 6.1.4 (7.0.0 available)
  flutter_native_splash 2.3.1 (2.3.2 available)
  intl 0.18.0 (0.18.1 available)
  matcher 0.12.15 (0.12.16 available)
  material_color_utilities 0.2.0 (0.8.0 available)
  petitparser 5.4.0 (6.0.1 available)
  pinput 2.2.31 (3.0.0 available)
  source_span 1.9.1 (1.10.0 available)
  stack_trace 1.11.0 (1.11.1 available)
  stream_channel 2.1.1 (2.1.2 available)
  test_api 0.5.1 (0.6.1 available)
  wechat_assets_picker 8.6.3 (8.7.0 available)
  wechat_camera_picker 3.8.0 (4.0.0 available)
  xml 6.3.0 (6.4.2 available)
Got dependencies!

还会有包提示,我们参考就是了。

太新的话要升级到 3.13.x 了,暂时不升级。

第三步:修改问题代码

修复 dio 包, HttpException 替换 DioError

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    // 200 请求成功, 201 添加成功
    if (response.statusCode != 200 && response.statusCode != 201) {
      handler.reject(
        DioException(
          requestOptions: response.requestOptions,
          response: response,
          type: DioExceptionType.badResponse,
        ),
        true,
      );
    } else {
      handler.next(response);
    }
  }
  @override
  Future<void> onError(
      DioException err, ErrorInterceptorHandler handler) async {
    final exception = HttpException(err.message ?? "error message");
    switch (err.type) {
      case DioExceptionType.badResponse: // 服务端自定义错误体处理
        {
          final response = err.response;
          final errorMessage = ErrorMessageModel.fromJson(response?.data);
          switch (errorMessage.statusCode) {
            // 401 未登录
            case 401:
              // 注销 并跳转到登录页面
              _errorNoAuthLogout();
              break;
            case 404:
              break;
            case 500:
              break;
            case 502:
              break;
            default:
              break;
          }
          Loading.error(errorMessage.message);
        }
        break;
      case DioExceptionType.unknown:
        break;
      case DioExceptionType.cancel:
        break;
      case DioExceptionType.connectionTimeout:
        break;
      default:
        break;
    }
    DioException errNext = err.copyWith(
      error: exception,
    );
    handler.next(errNext);
  }

最后:运行测试

woo

代码

https://ducafecat.com/course/flutter-woo

小结

如果你也有老版本需要升级,参考以下几点:

  • flutter pub upgrade 选一个合适的包版本
  • flutter pub get 排查包引用版本冲突
  • 有些包 api、使用方式发生很大变化,非必要不升级
  • 修复错误,仔细查看 api 接口申明
  • 修复冲突、警告、提示

感谢阅读本文

如果我有什么错?请在评论中让我知道。我很乐意改进。


© 猫哥 ducafecat.com

end