Flutter 3.22アップデートで起きたビルドエラーの解決方法 RangeError (offset): Invalid value: Not in inclusive range

こんにちは。エキサイト株式会社でアプリエンジニアをしている岡島です。Flutter SDKのバージョンアップをする際、iOSAndroidともにビルドができなくなりました。今回はなぜビルドができなくなってしまったのか、そのエラーの原因と解決方法について共有したいと思います。

はじめに

Flutter 3.22にアップデートを行った際に、APKビルド時に次のようなエラーが発生し、ビルドを完了することができませんでした。

Unhandled exception:
RangeError (offset): Invalid value: Not in inclusive range 0..1241: 10068
#0      RangeError.checkValueInInterval (dart:core/errors.dart:313)
#1      Source.getLocation (package:kernel/ast.dart:14992)
#2      Component.getLocation (package:kernel/ast.dart:14787)
#3      _getLocationInComponent (package:kernel/ast.dart:15181)
#4      Procedure._getLocationInEnclosingFile (package:kernel/ast.dart:3329)
#5      TreeNode._getLocationInEnclosingFile (package:kernel/ast.dart:207)
#6      TreeNode._getLocationInEnclosingFile (package:kernel/ast.dart:207)
#7      TreeNode.location (package:kernel/ast.dart:203)
#8      localFunctionName (package:vm/transformations/type_flow/utils.dart:449)
#9      SummaryCollector.createSummary (package:vm/transformations/type_flow/summary_collector.dart:628)
#10     TypeFlowAnalysis.getSummary (package:vm/transformations/type_flow/analysis.dart:1782)
#11     _DirectInvocation._processFunction (package:vm/transformations/type_flow/analysis.dart:398)
#12     _DirectInvocation.process (package:vm/transformations/type_flow/analysis.dart:287)
#13     _WorkList.processInvocation (package:vm/transformations/type_flow/analysis.dart:1663)
#14     _WorkList.process (package:vm/transformations/type_flow/analysis.dart:1606)
#15     TypeFlowAnalysis.process (package:vm/transformations/type_flow/analysis.dart:1811)
#16     transformComponent (package:vm/transformations/type_flow/transformer.dart:121)
#17     runGlobalTransformations (package:vm/kernel_front_end.dart:589)
#18     compileToKernel (package:vm/kernel_front_end.dart:489)
<asynchronous suspension>
#19     FrontendCompiler.compile (package:frontend_server/frontend_server.dart:639)
<asynchronous suspension>
#20     starter (package:frontend_server/starter.dart:101)
<asynchronous suspension>
#21     main (file:///b/s/w/ir/x/w/sdk/pkg/frontend_server/bin/frontend_server_starter.dart:13)
<asynchronous suspension>

Target kernel_snapshot failed: Exception


FAILURE: Build failed with an exception.

この記事では、こちらのエラーの解決方法について共有します。

環境

Dart 3.4.3
Flutter SDK: 3.22.2

解決した方法

同様のエラーを調べていく中でFlutterのissueに、エラーに関連する問題が報告されていました。

https://github.com/flutter/flutter/issues/148668

内容としては、このエラーはDartSDK側に原因があるようで、flutter_state_notifier:1.0.0を使用すると起こるエラーであることがわかりました。

こちらのコミットによってエラーが修正されたようなので、この修正が反映されているFlutterの3.23.0-0.1.preを使用することで対応することにしました。

まとめ

今回はFlutter SDKのアップデートを行う際に遭遇したエラーの解決をまとめました。SDKやライブラリのissueを見ることや詳細のログを見て原因を特定することの大切さを実感しました。