Flutterでのパッケージ管理

パッケージの追加

パッケージの追加はflutter pub addコマンドで行えます。

試しによく使われるだろうshared_preferencesを追加してみます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$ flutter pub add shared_preferences
╔════════════════════════════════════════════════════════════════════════════╗
║ A new version of Flutter is available! ║
║ ║
║ To update to the latest version, run "flutter upgrade". ║
╚════════════════════════════════════════════════════════════════════════════╝

Resolving dependencies...
async 2.8.2 (2.9.0 available)
collection 1.15.0 (1.16.0 available)
fake_async 1.2.0 (1.3.0 available)
+ ffi 1.1.2
+ file 6.1.2
flutter_lints 1.0.4 (2.0.0 available)
+ flutter_web_plugins 0.0.0 from sdk flutter
+ js 0.6.3 (0.6.4 available)
lints 1.0.1 (2.0.0 available)
material_color_utilities 0.1.3 (0.1.4 available)
path 1.8.0 (1.8.1 available)
+ path_provider_linux 2.1.5
+ path_provider_platform_interface 2.0.3
+ path_provider_windows 2.0.5
+ platform 3.1.0
+ plugin_platform_interface 2.1.2
+ process 4.2.4
+ shared_preferences 2.0.13
+ shared_preferences_android 2.0.11
+ shared_preferences_ios 2.1.0
+ shared_preferences_linux 2.1.0
+ shared_preferences_macos 2.0.3
+ shared_preferences_platform_interface 2.0.0
+ shared_preferences_web 2.0.3
+ shared_preferences_windows 2.1.0
source_span 1.8.1 (1.8.2 available)
test_api 0.4.8 (0.4.9 available)
vector_math 2.1.1 (2.1.2 available)
+ win32 2.5.1
+ xdg_directories 0.2.0+1
Downloading shared_preferences 2.0.13...
Downloading shared_preferences_platform_interface 2.0.0...
Downloading shared_preferences_macos 2.0.3...
Downloading shared_preferences_windows 2.1.0...
Downloading shared_preferences_web 2.0.3...
Downloading shared_preferences_linux 2.1.0...
Downloading shared_preferences_ios 2.1.0...
Downloading shared_preferences_android 2.0.11...
Downloading win32 2.5.1...
Changed 20 dependencies!

Flutter自体の更新を促されましたが、一旦はインストールできたようです。

パッケージの追加による変更内容の確認

ソースコード管理上差分として上がってくるのは以下の2ファイルでした。

1
2
3
4
5
6
7
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: pubspec.lock
modified: pubspec.yaml

pubspec.yaml

こちらには必要なパッケージの一覧が書かれています。今回追加したパッケージが差分として表示されています。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git diff pubspec.yaml 
diff --git a/pubspec.yaml b/pubspec.yaml
index 037db97..2c583dc 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -34,6 +34,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
+ shared_preferences: ^2.0.13

dev_dependencies:
flutter_test:

pubspec.lock

こちらはpubspec.yamlで指定したパッケージをインストールする場合の依存パッケージが具体的なバージョンとともに記載されています。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ git diff pubspec.lock 
diff --git a/pubspec.lock b/pubspec.lock
index 7e78bae..1da871d 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -57,6 +57,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
+ ffi:
+ dependency: transitive
+ description:
+ name: ffi
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "1.1.2"
+ file:
+ dependency: transitive
+ description:
...(省略)...

つまるところ、bundlerやpoetry、npmと同じような仕組みですね。

lib/generated_plugin_registrant.dart

.gitignoreに記載されているので差分には出てこないですが、libディレクトリの下にファイルが追加されています。このファイルを調べてみると、webアプリケーション向けのファイルのようです。

では一旦このままの状態でflutter runすると、問題なく起動しました。

次にファイルを削除してから起動してみます。すると、起動しないのかと思いきや問題なく起動しています。

なぜだろうと思ったのですが、みてみるとlibディレクトリの下に削除したgenerated_plugin_registrant.dartが作成されていました。自動的に作成されるので.gitignoreに記載されているということですね。

Flutterの更新

最初に指摘されていたFlutter自体の更新も行っておきます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
$ flutter upgrade
Upgrading Flutter to 2.10.4 from 2.10.3 in /Users/user/flutter...
Downloading Dart SDK from Flutter engine 57d3bac3dd5cb5b0e464ab70e7bc8a0d8cf083ab...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
1 209M 1 2270k 0 0 3404k 0 0:01:03 --:--:-- 0:01:03 3399k
7 209M 7 15.8M 0 0 9722k 0 0:00:22 0:00:01 0:00:21 9717k
13 209M 13 29.1M 0 0 10.9M 0 0:00:19 0:00:02 0:00:17 10.9M
20 209M 20 43.2M 0 0 11.7M 0 0:00:17 0:00:03 0:00:14 11.7M
27 209M 27 56.6M 0 0 12.1M 0 0:00:17 0:00:04 0:00:13 12.1M
33 209M 33 70.0M 0 0 12.3M 0 0:00:16 0:00:05 0:00:11 13.5M
39 209M 39 83.2M 0 0 12.4M 0 0:00:16 0:00:06 0:00:10 13.4M
45 209M 45 94.9M 0 0 12.3M 0 0:00:16 0:00:07 0:00:09 13.1M
50 209M 50 106M 0 0 12.3M 0 0:00:17 0:00:08 0:00:09 12.6M
56 209M 56 118M 0 0 12.2M 0 0:00:17 0:00:09 0:00:08 12.3M
62 209M 62 130M 0 0 12.2M 0 0:00:17 0:00:10 0:00:07 12.0M
67 209M 67 141M 0 0 12.1M 0 0:00:17 0:00:11 0:00:06 11.6M
73 209M 73 154M 0 0 12.2M 0 0:00:17 0:00:12 0:00:05 11.9M
80 209M 80 168M 0 0 12.2M 0 0:00:17 0:00:13 0:00:04 12.2M
86 209M 86 181M 0 0 12.3M 0 0:00:16 0:00:14 0:00:02 12.5M
93 209M 93 194M 0 0 12.4M 0 0:00:16 0:00:15 0:00:01 12.9M
99 209M 99 208M 0 0 12.4M 0 0:00:16 0:00:16 --:--:-- 13.2M
100 209M 100 209M 0 0 12.4M 0 0:00:16 0:00:16 --:--:-- 13.3M
Building flutter tool...

Upgrading engine...
Downloading android-arm-profile/darwin-x64 tools... 628ms
Downloading android-arm-release/darwin-x64 tools... 480ms
Downloading android-arm64-profile/darwin-x64 tools... 500ms
Downloading android-arm64-release/darwin-x64 tools... 477ms
Downloading android-x64-profile/darwin-x64 tools... 492ms
Downloading android-x64-release/darwin-x64 tools... 455ms
Downloading android-x86 tools... 1,246ms
Downloading android-x64 tools... 1,583ms
Downloading android-arm tools... 1,240ms
Downloading android-arm-profile tools... 618ms
Downloading android-arm-release tools... 452ms
Downloading android-arm64 tools... 1,600ms
Downloading android-arm64-profile tools... 662ms
Downloading android-arm64-release tools... 478ms
Downloading android-x64-profile tools... 666ms
Downloading android-x64-release tools... 489ms
Downloading android-x86-jit-release tools... 705ms
Downloading ios tools... 4.8s
Downloading ios-profile tools... 4.4s
Downloading ios-release tools... 18.3s
Downloading Web SDK... 4.6s
Downloading CanvasKit... 1,524ms
Downloading package sky_engine... 228ms
Downloading flutter_patched_sdk tools... 366ms
Downloading flutter_patched_sdk_product tools... 756ms
Downloading darwin-x64 tools... 3.3s
Downloading darwin-x64/font-subset tools... 338ms

Flutter 2.10.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision c860cba910 (13 days ago) • 2022-03-25 00:23:12 -0500
Engine • revision 57d3bac3dd
Tools • Dart 2.16.2 • DevTools 2.9.2

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.4, on macOS 11.6.4 20G417 darwin-x64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.66.0)
[✓] Proxy Configuration
[✓] Connected device (1 available)
[✓] HTTP Host Availability

• No issues found!

最後にflutter doctorを実行して確認してくれていますね。

まとめ

Flutterのバージョン管理は確立した仕組みがあり、とても頼もしいです。このあたりで特に問題になることはなさそうですね。

参考図書