flutter:依赖问题

依赖的使用

Flutter 依赖是什么?

在 Flutter 项目中,第三方库(package/plugin)就是 依赖,它们写在 pubspec.yaml 文件里。

比如:

dependencies:
  flutter:
    sdk: flutter
  http: ^1.2.0   # 一个网络请求库
  provider: ^6.0.5  # 状态管理库

分类:

  • 官方依赖:由 Flutter 团队提供(如 flutter, cupertino_icons)。
  • 第三方依赖:来自 pub.dev 的社区库(如 http, provider)。
  • 本地依赖:存放在项目目录里的库。
  • Git 依赖:直接从 Git 仓库拉取的库。

下载依赖

依赖声明后需要下载:

方式一:自动下载

执行:flutter pub get

作用:

  • 根据 pubspec.yaml 的依赖列表下载 package。
  • 下载后的依赖会存放在 缓存目录(一般是 ~/.pub-cache)。
  • 在项目目录下生成/更新 pubspec.lock 文件,锁定依赖版本。

方式二:直接添加依赖

可以用命令行添加依赖:

  • 下载到dependencies:flutter pub add 包名

  • 下载到dev_dependencies:

    • flutter pub add --dev 包名
    • flutter pub add -d 包名

会自动修改 pubspec.yaml 并下载依赖。

更新依赖

  • 更新依赖到符合 pubspec.yaml 里的版本范围(所有依赖):

    flutter pub upgrade
    
  • 只更新某个依赖:

    flutter pub upgrade http
    

删除依赖

  • 手动删除:在 pubspec.yaml 里删掉对应依赖,然后执行:flutter pub get

    • 此时该依赖和相关文件就不会再被使用。
    • 但是缓存中仍会保留下载过的版本,以便下次使用时更快,不会自动清除
  • 彻底清理缓存flutter pub cache clean

    • 会把本地缓存清掉,下次会重新下载。

管理依赖版本

pubspec.yaml 里可以指定依赖的版本范围:

  • 固定版本

    http: 1.2.0
    
  • 推荐写法(兼容更新)

    http: ^1.2.0
    

    代表:>=1.2.0 <2.0.0 。表示 最低版本是 1.2.0,最高版本 < 2.0.0

    flutter_native_splash: ^2.4.2
    

    flutter_native_splash的最高版本是2.4.6。所有经过我这里写的是2.4.2,当我执行flutter pub get的时候会自动更新到2.4.6,而不是自己想要的2.4.2

    如果自己想要,就必须写死版本:

    flutter_native_splash: 2.4.2
    
  • 任意版本:(不推荐,可能导致不可控的错误)

    http: any
    
  • Git 仓库依赖

    my_package:
      git:
        url: https://github.com/xxx/my_package.git
        ref: main
    
  • 本地依赖

    my_local_package:
      path: ../my_local_package
    

常见命令总结

命令 作用
flutter pub get 根据 pubspec.yaml 下载依赖
flutter pub add 包名 添加依赖并下载
flutter pub upgrade 更新所有依赖到最新符合范围的版本
flutter pub remove 包名 删除依赖
flutter pub outdated 查看哪些依赖有新版本
flutter pub cache clean 清空依赖缓存

两种依赖

dependencies

  • 作用:存放项目 运行时需要的依赖
  • 在应用运行时(真机/模拟器/发布后),这些依赖会被打包进应用。
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.8
  package_info_plus: ^8.3.1
  get: ^4.7.2
  shared_preferences: ^2.5.3
  dio: ^5.9.0
  adaptive_theme: ^3.7.1+2

特点

  • 用户在安装/使用 app 时,这些依赖会跟随你的代码一起运行。
  • Flutter 应用的核心功能依赖一定要放在这里。

dev_dependencies

  • 作用:存放项目 开发/测试阶段需要的依赖
  • 在应用 运行时不会被打包进去,只在开发环境中用。
dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^5.0.0
  icons_launcher: ^3.0.2
  flutter_native_splash: 2.4.2

特点

  • 不影响 app 运行。
  • 只在开发、构建、测试阶段使用。
  • dev_dependencies:唯一需要记住的命令区别是 flutter pub add --dev。出来这个,其它命令(getremoveupgrade 等)都会同时处理 dependenciesdev_dependencies

个人使用依赖时遇到的问题

确定依赖的版本

依赖树

由于在pubspec.yaml许多依赖都是这么写的:

dependencies:
  flutter:
    sdk: flutter
  http: ^1.2.0   # 一个网络请求库
  provider: ^6.0.5  # 状态管理库

都只是知道最小版本,具体的版本我是不知道的。如果想知道,可以查看依赖树

命令 作用
flutter pub deps 查看依赖树(哪些包依赖了哪些包)
flutter pub deps --style=compact 紧凑格式查看依赖树
flutter pub deps --json 以 JSON 格式输出依赖关系(适合脚本处理)
(base) xieshaolin@xieshaolindeMacBook-Pro flutter_sophomore % flutter pub deps
Dart SDK 3.8.1
Flutter SDK 3.32.8
flutter_sophomore 1.0.0+1
├── adaptive_theme 3.7.1+2
│   ├── flutter...
│   └── shared_preferences...
├── cupertino_icons 1.0.8
├── dio 5.9.0
│   ├── async 2.13.0
│   │   ├── collection...
│   │   └── meta...
│   ├── dio_web_adapter 2.1.1
│   │   ├── dio...
│   │   ├── http_parser...
│   │   ├── meta...
│   │   └── web...
│   ├── http_parser 4.1.2
│   │   ├── typed_data 1.4.0
│   │   │   └── collection...
│   │   ├── collection...
│   │   ├── source_span...
│   │   └── string_scanner...
│   ├── mime 2.0.0
│   ├── collection...
│   ├── meta...
│   └── path...
├── flutter 0.0.0
│   ├── characters 1.4.0
│   ├── collection 1.19.1
│   ├── material_color_utilities 0.11.1
│   │   └── collection...
│   ├── meta 1.16.0
│   ├── sky_engine 0.0.0
│   └── vector_math 2.1.4
├── flutter_lints 5.0.0
│   └── lints 5.1.1
├── flutter_native_splash 2.4.2
│   ├── ansicolor 2.0.3
│   ├── html 0.15.6
│   │   ├── csslib 1.0.2
│   │   │   └── source_span...
│   │   └── source_span...
│   ├── xml 6.6.1
│   │   ├── petitparser 7.0.1
│   │   │   ├── collection...
│   │   │   └── meta...
│   │   ├── collection...
│   │   └── meta...
│   ├── args...
│   ├── flutter...
│   ├── flutter_web_plugins...
│   ├── image...
│   ├── meta...
│   ├── path...
│   ├── universal_io...
│   └── yaml...
├── flutter_test 0.0.0
│   ├── boolean_selector 2.1.2
│   │   ├── source_span...
│   │   └── string_scanner...
│   ├── fake_async 1.3.3
│   │   ├── clock...
│   │   └── collection...
│   ├── leak_tracker 10.0.9
│   │   ├── clock...
│   │   ├── collection...
│   │   ├── meta...
│   │   ├── path...
│   │   └── vm_service...
│   ├── leak_tracker_flutter_testing 3.0.9
│   │   ├── flutter...
│   │   ├── leak_tracker...
│   │   ├── leak_tracker_testing...
│   │   ├── matcher...
│   │   └── meta...
│   ├── leak_tracker_testing 3.0.1
│   │   ├── leak_tracker...
│   │   ├── matcher...
│   │   └── meta...
│   ├── matcher 0.12.17
│   │   ├── async...
│   │   ├── meta...
│   │   ├── stack_trace...
│   │   ├── term_glyph...
│   │   └── test_api...
│   ├── source_span 1.10.1
│   │   ├── collection...
│   │   ├── path...
│   │   └── term_glyph...
│   ├── stack_trace 1.12.1
│   │   └── path...
│   ├── stream_channel 2.1.4
│   │   └── async...
│   ├── string_scanner 1.4.1
│   │   └── source_span...
│   ├── term_glyph 1.2.2
│   ├── test_api 0.7.4
│   │   ├── async...
│   │   ├── boolean_selector...
│   │   ├── collection...
│   │   ├── meta...
│   │   ├── source_span...
│   │   ├── stack_trace...
│   │   ├── stream_channel...
│   │   ├── string_scanner...
│   │   └── term_glyph...
│   ├── vm_service 15.0.0
│   ├── async...
│   ├── characters...
│   ├── clock...
│   ├── collection...
│   ├── flutter...
│   ├── material_color_utilities...
│   ├── meta...
│   ├── path...
│   └── vector_math...
├── get 4.7.2
│   ├── flutter...
│   └── web...
├── icons_launcher 3.0.2
│   ├── args 2.7.0
│   ├── image 4.5.4
│   │   ├── archive 4.0.7
│   │   │   ├── crypto 3.0.6
│   │   │   │   └── typed_data...
│   │   │   ├── posix 6.0.3
│   │   │   │   ├── ffi...
│   │   │   │   ├── meta...
│   │   │   │   └── path...
│   │   │   └── path...
│   │   ├── meta...
│   │   └── xml...
│   ├── universal_io 2.2.2
│   │   ├── collection...
│   │   ├── meta...
│   │   └── typed_data...
│   ├── yaml 3.1.3
│   │   ├── collection...
│   │   ├── source_span...
│   │   └── string_scanner...
│   └── path...
├── package_info_plus 8.3.1
│   ├── clock 1.1.2
│   ├── ffi 2.1.4
│   ├── flutter_web_plugins 0.0.0
│   │   ├── characters...
│   │   ├── collection...
│   │   ├── flutter...
│   │   ├── material_color_utilities...
│   │   ├── meta...
│   │   └── vector_math...
│   ├── http 1.5.0
│   │   ├── async...
│   │   ├── http_parser...
│   │   ├── meta...
│   │   └── web...
│   ├── package_info_plus_platform_interface 3.2.1
│   │   ├── plugin_platform_interface 2.1.8
│   │   │   └── meta...
│   │   ├── flutter...
│   │   └── meta...
│   ├── path 1.9.1
│   ├── web 1.1.1
│   ├── win32 5.14.0
│   │   └── ffi...
│   ├── flutter...
│   └── meta...
└── shared_preferences 2.5.3
    ├── shared_preferences_android 2.4.11
    │   ├── flutter...
    │   └── shared_preferences_platform_interface...
    ├── shared_preferences_foundation 2.5.4
    │   ├── flutter...
    │   └── shared_preferences_platform_interface...
    ├── shared_preferences_linux 2.4.1
    │   ├── file 7.0.1
    │   │   ├── meta...
    │   │   └── path...
    │   ├── path_provider_linux 2.2.1
    │   │   ├── xdg_directories 1.1.0
    │   │   │   ├── meta...
    │   │   │   └── path...
    │   │   ├── ffi...
    │   │   ├── flutter...
    │   │   ├── path...
    │   │   └── path_provider_platform_interface...
    │   ├── path_provider_platform_interface 2.1.2
    │   │   ├── platform 3.1.6
    │   │   ├── flutter...
    │   │   └── plugin_platform_interface...
    │   ├── flutter...
    │   ├── path...
    │   └── shared_preferences_platform_interface...
    ├── shared_preferences_platform_interface 2.4.1
    │   ├── flutter...
    │   └── plugin_platform_interface...
    ├── shared_preferences_web 2.4.3
    │   ├── flutter...
    │   ├── flutter_web_plugins...
    │   ├── shared_preferences_platform_interface...
    │   └── web...
    ├── shared_preferences_windows 2.4.1
    │   ├── path_provider_windows 2.3.0
    │   │   ├── ffi...
    │   │   ├── flutter...
    │   │   ├── path...
    │   │   └── path_provider_platform_interface...
    │   ├── file...
    │   ├── flutter...
    │   ├── path...
    │   ├── path_provider_platform_interface...
    │   └── shared_preferences_platform_interface...
    └── flutter...

依赖树+grep

上面的依赖树会答应非常多的内容,可以用grep去筛选。

flutter pub deps | grep flutter_native_splash 
 ├── flutter_native_splash 2.4.6

×

喜欢就点赞,疼爱就打赏