Skip to content

crashes with empty Map, causes type cast error #1806

Closed

Description

@jason-downs

Describe the bug

According to the discussion here: #1799 having Map types with int keys is already supported even though the documentation has not yet been updated to reflect this.

However, hen using a serverpod-serializable class as defined in the yaml files, if a field is type Map with int keys (or enum keys whether they are serialized by name or by number) and then use it in any situation where this Map contains no entries and it needs to be deserialized, it triggers an exception.

The offending code appears to be in the src/generated/protocol.dart file where during deserialization, the Map data attempts to be type-cast from a Map to a List, as seen in this screenshot (note the "data as List"):

Screenshot 2024-01-23 at 2 16 21 PM

...which produces the error:

type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast

Looking into things, it seems that map data is being stored behind the scenes in a format that is a List of map entries, with format:
`[{"k": somekey, "v":somevalue}, {"k": anotherkey, "v": anothervalue}]. So it would seem that since it's actually a list in that case, the type cast in question works when data exists and has been converted into that format, but when there is no data in the map, it remains a Map type and the type cast fails.

In one case, this was a database table class and I was able to see in my database viewer that the empy Map field was written to the database as {} whereas the non-empty ones were written as the list format described above. Changing these empty brackets from {} to [] (map to list) and then re-running caused the app to run without this error. In another case, adding some placeholder value in the Map on startup also avoided the error. However, these are hacky workarounds and there are many cases where you might want some Map data type that is initially empty and then filled in by various user actions, for example.

This was tested in two different projects with various types of Maps with the same result.

To Reproduce
Steps to reproduce the behavior:

  1. Define a class with one or more Map fields with int keys:
Screenshot 2024-01-23 at 2 33 40 PM
  1. Write an endpoint function that handles this data in some way:
Screenshot 2024-01-23 at 2 34 05 PM
  1. Call this endpoint when the Map field is empty:
Screenshot 2024-01-23 at 2 34 49 PM
  1. Error:
Screenshot 2024-01-23 at 2 35 02 PM

Serverpod versions

All serverpod components are 1.2.1

Platform information

Flutter 3.16.8, Dart 3.2.5, MacOS Sonoma 14.2.1, serverpod server running on localhost

Activity

  1. Isakdl commented on Jan 24, 2024

    @Isakdl
    Collaborator

    Thank you for the detailed issue! I will cover this case in the PR you linked!

  2. jason-downs commented on Jul 1, 2025

    @jason-downs
    Author

    Unfortunately the problems with empty Map types haven't been fully fixed even with this PR merged; see #1908

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions