【flutter】「fatal error: module 'XXX' not found @import XXX; 」の解決方法

みなさんこんにちは、 id:takkumattsu です!

最近は本当に flutter での開発が多く、また結構楽しい感じがしています!
iOS、Android のアプリを作るなら flutter はかなりいい選択肢ですね

f:id:takkumattsu:20210629133214p:plain:w360

今日は flutter の iOS ビルドで時々起こる fatal error: module 'XXX' not found @import XXX について自分が起きた際の解決方法を書いていきたいと思います。

そもそもこのエラーについて

fatal error: module 'XXX' not found @import XXX は iOSでビルドした際に起きるエラーになります。
自分は image_cropper | Flutter Package というライブラリを入れた時に起きました。

pub.dev

解決方法

解決方法 Podfileの platform :ios, '9.0' のところをいじる。
(コメントアウトを外してビルドしたりバージョンを上げたり下げたり)

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

おそらくこのfatal error: module 'XXX' not found @import XXXは対応するPodのライブラリが iOS Deployment Target に対応していないと起こるようです。

自分の場合

自分の場合は firebase_core | Flutter Package の 1.3.0を入れた時にデフォルト( # platform :ios, '9.0' がコメントアウトされた状態) だとエラーになったため、platform :ios, '12.0'としていました。

diff --git a/ios/Podfile b/ios/Podfile
index 2c068c4..1e8c3c9 100644
--- a/ios/Podfile
+++ b/ios/Podfile
@@ -1,5 +1,5 @@
 # Uncomment this line to define a global platform for your project
-platform :ios, '12.0'
+# platform :ios, '9.0'

この状態でimage_cropper | Flutter Packageを入れてビルドすると以下のようなエラーが出ていました。

[12:53:06]: Exit status of command 'fvm flutter build ios --release' was 1 instead of 0.
Changing current working directory to: /Users/takkumattsu/workspace/アプリ名
Building バンドルID for device (ios-release)...
Automatically signing iOS for device deployment using specified development team in Xcode project: チームID
Running pod install...                                              7.7s
Running Xcode build...
Xcode build done.                                           164.4s
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    4 warnings generated.
    1 warning generated.
    4 warnings generated.
    1 warning generated.
    /Users/takkumattsu/workspace/アプリ名/ios/Runner/GeneratedPluginRegistrant.m:22:9: fatal error: module 'image_cropper' not found
    @import image_cropper;
     ~~~~~~~^~~~~~~~~~~~~

それを以下のようにしたところエラーが取れました。

diff --git a/ios/Podfile b/ios/Podfile
index 9411102..2c068c4 100644
--- a/ios/Podfile
+++ b/ios/Podfile
@@ -1,5 +1,5 @@
 # Uncomment this line to define a global platform for your project
-platform :ios, '10.0'
+platform :ios, '12.0'

この辺は入ってるライブラリによって変わってくると思うのでどのバージョンで直るかはわからないです。

最後に

flutter での開発は結構楽しくて好きなんですが、Xcode 周りでのエラーがかなりわかりづらくハマりやすいのがちょっと難点な気がしています‥
(自分はめちゃくちゃハマりました‥)