[Flutter]Dart Error: error: import of dart:mirrors is not supported in the current Dart runtimeの対処法

このページには広告が含まれる場合があります。

 

これは結構トラップ的な話なのですが、Flutterのアプリを起動させると、こんなエラーが出て画面が固まって動かなくなってしまうことがあります。

E/flutter (23085): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime

この状態でハマってしまった場合の対処法です。

(以下のStackOverFlowを参考にさせて頂きました)

 

JsonSerializableパッケージを「dev_dependencies」に移動させれば解決

これは、結論から言うと誰が悪さをしているのかというと、JsonをDartのクラスに変換してくれる「JsonSerializable」パッケージなんです。

このパッケージを、公式では「pubspec.yaml」ファイルの「dependencies」セクションに記述するよう記載があるのですが、これをやってしまうとダメな場合があるようなんです。

I assume that you want to use json_serializable for your own sourceCode creation. If this is the case you should have json_serializable: ^2.0.0 under the dev_dependencies.

Explanation

json_serializable does use dartlang/source_gen internally. source_gen in turn does reference dart:mirrors, which is not supported in flutter (as told in this post).

So long story in short: – through removing the dependency on json_serializable from your “build” dependencies you remove the dependency on mirrors which is stopping your flutterBuild.

つまり、「JsonSerializable」パッケージを使って自分のプロジェクトでコード生成を行っているような場合は、公式に記載の「dependencies」ではなくて「dev_dependencies」セクションに記述する必要があるようなんです。

(JsonSerializableが内部的に「source_gen」というコード生成パッケージを参照していて、その「source_gen」が「dart:mirrors」というパッケージを参照しているのだが、これがFlutterではサポートされていないためにエラーになったとのこと)

ですので、

1)「JsonSerializable」パッケージを「pubspec.yaml」ファイルの「dependencies」から「dev_dependencies」セクションに移動

Dependencies fall into one of two types. Regular dependencies are listed under dependencies:—these are packages that anyone using your package will also need. Dependencies that are only needed in the development phase of the package itself are listed under dev_dependencies.

2)「JsonSerializable」を使っているdartファイルでimport分を削除

すれば解消します。

(詳しくは、下の動画で解説していますので、よろしければご覧ください)

 

こんな記事も読まれています

GAMBO(願望) 願望実現・目標達成の
17秒 / 68秒ワーク
GAMBO(願望) 引き寄せ難民だったぼくの挫折経験から生まれた誰でもできる願望実現サポートアプリ。かの有名な17秒/68秒ワークを独自の視覚化メソッドで簡単実践。
App Store / Google Play 詳しく見る →
Meiso(瞑想) 鐘音と自然音で
5分からのマインドフルネス
Meiso(瞑想) 初心者でも“迷わず続けられる”ことにこだわった、完全無料の瞑想アプリ。画面のガイドに従うだけで呼吸のリズムが整い、最短5分から瞑想を実践できます。
App Store / Google Play 詳しく見る →
小学生からの英検®単語 A ふりがな・音声付き
英検単語1日5分トレーニング
小学生からの英検®単語 ありそうでなかった!ふりがな付き英検®単語トレーニングアプリ!1日たった5分!スキマ時間を活用して、英検®頻出単語を効率的に習得。お子様の一人学習にも安心設計。
App Store / Google Play 詳しく見る →
オボエルンジャー レトロゲームで
nバック脳トレ
オボエルンジャー 科学的脳力開発法「Nバック課題」をファミコン風8bitレトロゲームにアレンジした新感覚脳トレアプリ。記憶力・感情コントロール力・集中力の向上に。
App Store / Google Play 詳しく見る →
ゴイコイコイ 超高速フラッシュカードで
語彙習得
ゴイコイコイ 日常語1400語を視覚的に学べる、高速読み上げフラッシュカード式の語彙トレーニングアプリ。知育教室で行われるフラッシュカード学習を、家庭で“手軽に・安価に・正確に”再現。
App Store / Google Play 詳しく見る →

TOP