Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
projectitis opened this issue Aug 26, 2023 · 2 comments
Closed
Assignees
Labels

Comments

@projectitis
Copy link
Contributor

projectitis commented Aug 26, 2023

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?

@projectitis projectitis changed the title bug: AssetCache.readJson fails for json files where the root object is an array bug: AssetCache.readJson fails for json files where the root is an array Aug 26, 2023
@spydon
Copy link
Member

spydon commented Aug 26, 2023

Sounds good, I'll assign you to the issue

@spydon
Copy link
Member

spydon commented Sep 8, 2023

Closing this meanwhile, since it is added to the #1938 list

@spydon spydon closed this as completed Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants