1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter: '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'エラーの回避

Posted at

状況

ローカルストレージライブラリ hive にJSON形式でデータを保存した場合、Dartの型としては Map<String, dynamic>になるが、これをHiveから読み出し→fromJsonでデコードしようとしたところ表題のエラーが発生していた。

もしかしたらFirebaseのsnapshotを受け取る場面でも同様のエラーに遭遇するかも知れない。

final box = await Hive.openBox('hoge');
// 同じ形式のJSONデータが複数件保存されている前提
// この時点でjsonListはList<Map<dynamic>>
final jsonList = box.values.toList();
// 取得した情報でMapを構築し、そこからHogeオブジェクトを生成しList化
return jsonList
      .map((e) => Hoge.fromJson(e))
      .toList();

対処

Listに格納された要素をmapメソッドで処理するコードだが、HogeオブジェクトをfromJsonメソッドで作ろうとしたが、Map<dynamic, dynamic> から Map<String, dynamic> に暗黙的に変換できないぞ、ということらしい。

うまくキャストしてやれば良いのだが、書き方としてはこうなる。
Map<String, dynamic>.from(e)

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?