Skip to content

Failure on page reload #182

Closed
Closed

Description

@boosey

First load of my Flutter web app utilizing flutter_data works just fine. If I reload the page via the web browser, I receive the following:

[flutter_data] Box failed to open:
Expected a value of type 'Map<String, dynamic>', but got one of type 'LinkedMap<dynamic, dynamic>

I've searched but cannot find this issue answered anywhere.

Activity

  1. boosey commented on Nov 8, 2022

    @boosey
    Author

    Has anyone seen this error before? Is there more information that I should provide to help diagnose?

    Here are my dependencies:

      cupertino_icons: ^1.0.2
      flutter_data: ^1.4.7
      json_annotation: ^4.7.0
      copy_with_extension: ^4.0.4
      build_runner: ^2.3.2
      json_serializable: ^6.5.4
      path_provider: ^2.0.11
      flutter_riverpod: ^1.0.3+1
    
  2. frank06 commented on Nov 8, 2022

    @frank06
    Contributor

    I am reluctantly supporting Flutter Web because it's awful. Looks like a wrong cast, I'll try to reproduce soon.

  3. boosey commented on Nov 8, 2022

    @boosey
    Author

    As I was tracing my code, I came across this observation. On the initial load in the model.g.dart file, the data getting passed in is a _jsonMap. On the reload, the data coming in is a Map<String, dynamic>.

    I assume the difference is that the first load comes from the API and the second one comes from the Hive Box? But it seems like it should always pass the same type. I thought the first load would be loaded into Hive and my code actually accessed that Hive Box. But there is definitely a difference of the types depending on which load is occurring.

    Also, I realized I may have omitted a key piece of information. My model is a composite. In other words, the top level object contains a list of sub-objects. These are not sub-resources. There is no API to access them independently from the top level resource. When the top level resource is requested it contains the complete list of the sub-objects.

    The top level is @DataRepository and @JsonSerializable. But the sub-object is just @JsonSerializable because I didn't need the framework to treat them as a indpendent resource. The failure is actually occuring on the sub-object (Layer) deserialization. I have attached a portion of my code to illustrate:

    In case it is relevant, the database is MongoDB.

    @JsonSerializable(includeIfNull: false, explicitToJson: true)
    @CopyWith()
    @DataRepository([ModelAdapter])
    class Model extends DataModel<Model> {
      Model({
        this.id,
        required this.name,
        this.layers,
      });
    
      @override
      final String? id;
      final String name;
      late List<Layer>? layers;
    }
    
    @JsonSerializable(includeIfNull: false, explicitToJson: true)
    class Layer {
      Layer({ required this.id, required this.name, });
    
      final String id;
      final String name;
    
      // ignore: sort_constructors_first
      factory Layer.fromJson(Map<String, dynamic> json) => _$LayerFromJson(json);
    
      Map<String, dynamic> toJson() => _$LayerToJson(this);
    }
    
  4. frank06 commented on Nov 9, 2022

    @frank06
    Contributor

    I should be able to look at this next week. It would be great if you found a workaround in the meantime.

  5. boosey commented on Nov 17, 2022

    @boosey
    Author

    Any progress? I did find a workaround. I have to sync local storage all the time. For some reason, when syncing, it takes as long to hot reload as it does to stop and start the application in debug.

  6. frank06 commented on Nov 22, 2022

    @frank06
    Contributor

    No, I couldn't look at this in detail yet.

    What do you mean by "sync local storage all the time"? Can you share some code? Maybe that gives me a pointer to fix this faster.

  7. frank06 commented on Nov 22, 2022

    @frank06
    Contributor
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