たろすの技術メモ

Jot Down the Tech

ソフトウェアエンジニアのメモ書き

Flutter v3.0.5 × mobile_scanner v3.0.0-beta.2でビルドする為の回避策

※この記事はFlutter v3.0.5 × mobile_scanner v3.0.0-beta.2でビルドする為の回避策の記事をエクスポートしたものです。内容が古くなっている可能性があります。

概要

Flutter v3.0.5で開発中のプロジェクトでmobile_scannerのBeta版であるv3.0.0-beta.2を使用するとビルドできない問題が発生しましたが、fork版で回避できたので備忘録を残しておきます。

https://pub.dev/packages/mobile_scanner/versions/3.0.0-beta.2

前提

参画しているプロジェクトで使用しているmobile_scannerの挙動が不安定で色々対策を講じていたのですが、2022/11/18にv3.0.0-beta.2がリリースされていたので少しでも改善されていることを願って検証することにしました。しかし表題の問題が発生し、さらにエラーの原因がPlugin側でした。

解決方法

Flutter v3.3.0以上を使用することで根本解決できます。しかし今回のプロジェクトではまだFlutter v3.0.5を使用する必要がある && 検証用なのでfork版を作成して回避することにしました。その回避方法を以下に記載します。

エラー内容

Launching lib/main.dart on Pixel 3a in debug mode...
lib/main.dart:1
Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01
Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01
: Error: 'Uint8List' isn't a type.
../…/src/mobile_scanner_controller.dart:283
            image: event['image'] as Uint8List?,
                                     ^^^^^^^^^
FAILURE: Build failed with an exception.

* Where:
Script '/Users/r/fvm/versions/3.0.5/packages/flutter_tools/gradle/flutter.gradle' line: 1156

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/Users/r/fvm/versions/3.0.5/bin/flutter'' finished with non-zero exit value 1

* 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 30s
Exception: Gradle task assembleDebug failed with exit code 1
Exited

以下の部分が原因です。

: Error: 'Uint8List' isn't a type.
../…/src/mobile_scanner_controller.dart:283
            image: event['image'] as Uint8List?,
                                     ^^^^^^^^^

回避方法

以下のリポジトリか、自身で作成したfork版を使用することで回避できます。(../…/src/mobile_scanner_controller.dartimport 'dart:typed_data';を追加するだけです。)

https://github.com/taroooth/mobile_scanner/tree/fix-import-error-in-mobile-scanner-controller

fork版Pluginの使用方法

mobile_scanner:
    git:
      url: https://github.com/taroooth/mobile_scanner
      ref: fix-import-error-in-mobile-scanner-controller

経緯

エラー文の通り../…/src/mobile_scanner_controller.dartを確認してみるとimport 'dart:typed_data';が記述されていないからだと分かりました。「Beta版とはいえビルドできないものを公開するかなぁ」と思いながらもPluginのGitHubを確認してみると、以下のcommitを発見しました。

何か意図があって削除していることが分かりましたが、該当するPull Request(以下: PR)が無かったのでとりあえずforkしてFlutter v3.0.5で動くようにimport文を追記しました。このfork版を使用すると問題なくビルドできるようになりました。

https://github.com/taroooth/mobile_scanner/tree/fix-import-error-in-mobile-scanner-controller

issueを立てても良かったのですが、fork版も作ってしまったのでPRを投げてみました。

https://github.com/juliansteenbakker/mobile_scanner/pull/383

すると作者から以下のコメントを頂き、原因はFlutterのバージョンが最新ではないからだということがわかりました。

This is because you are running an outdated version of Flutter. I should update the minimum required flutter version to fix this, because otherwise the build proces will fail because if i add this import, it will generate a warning. For now i suggest using the latest version of the Flutter Stable release, which is 3.3.9.

これは、Flutterのバージョンが古いためです。この問題を解決するために、最低限必要なFlutterのバージョンを更新する必要があります。そうしないと、このインポートを追加したときに警告が発生して、ビルドプロセスが失敗してしまうからです。 今のところ、Flutterの安定版である3.3.9の最新バージョンを使うことをお勧めします。

手元で確認するとFlutter v3.3.0で問題なくビルドできました。v3.3.0でimport 'dart:typed_data';すると以下の警告文が表示されます。

The import of 'dart:typed_data' is unnecessary because all of the used elements are also provided by the import of 'package:flutter/foundation.dart'.

作者はこの警告を消すためにimport 'dart:typed_data';を削除したのだな、ということが分かりました。

まとめ

これまでPluginは基本的にStableバージョンを使っていたのでPlugin側のエラーでビルドできないことに驚きましたが、今後はBeta版では(利用者側の開発環境によっては)そういったことも起こり得るということを念頭に置いて対処していきたいです。