Skip to content

bug: AssetCache.readJson fails for json files where the root is an array #2682

@projectitis

Description

@projectitis

Current bug behavior

Reading a JSON file using AssetCache.readJson results in an error if the JSON file root element is an array.

An example JSON file that meets this criteria is

[
    "The root of this JSON file is an array",
    "Which is perfectly legal JSON"
]

The error is

_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast)

This is because it attempts to cast the result to a Map<String, dynamic>, however, a JSON file can also be a List<dynamic>.

Expected behavior

AssetCache.readJson should return a Future<dynamic> (It uses jsonDecode under the hood, which returns dynamic).

Steps to reproduce

  1. Include the JSON file above in the assets (and your pubspec)
  2. ensure you import 'package:flame/cache.dart';
  3. call AssetsCache().readJson('test.json')

More environment information

  • Flame version latest
  • Affects all platforms

More information

I can provide the fix. It's very simple and I think it will be backwards compatible in most cases?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions