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

Failure on page reload #182

Closed
boosey opened this issue Nov 7, 2022 · 7 comments
Closed

Failure on page reload #182

boosey opened this issue Nov 7, 2022 · 7 comments

Comments

@boosey
Copy link

boosey commented Nov 7, 2022

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.

@boosey
Copy link
Author

boosey commented Nov 8, 2022

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

@frank06
Copy link
Contributor

frank06 commented Nov 8, 2022

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

@boosey
Copy link
Author

boosey commented Nov 8, 2022

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);
}

@frank06
Copy link
Contributor

frank06 commented Nov 9, 2022

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

@boosey
Copy link
Author

boosey commented Nov 17, 2022

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.

@frank06
Copy link
Contributor

frank06 commented Nov 22, 2022

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.

@frank06
Copy link
Contributor

frank06 commented Nov 22, 2022

#185

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants