[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分を削除

すれば解消します。

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

 

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

今すぐ無料お試しキットを受け取る

はじめての方へ


みんプロ式プログラミング講座体系・ラインナップ
みんプロ式プログラミング講座受講の流れ(割引特典利用方法)
サイトマップ(みんプロHPコンテンツ一覧)
運営会社概要

みんプロ作成アプリ





今すぐ無料お試しキットを受け取る
TOP