MichaelMarner / dart-redux-remote-devtools

Remote Devtools for Dart & Flutter
https://pub.dartlang.org/packages/redux_remote_devtools
MIT License
52 stars 10 forks source link

replace/rename `toJson` to something else #38

Closed chirastefan closed 1 year ago

chirastefan commented 1 year ago

Is there a way to replace or rename this method? Context: I'm using https://pub.dev/packages/dart_mappable which generates toJson(returns String) and toMap(returns Map<String,dynamic>) on a decorated class. What I need from that class is toMap which is what dart-redux-remote-devtools expects toJson to return;

MichaelMarner commented 1 year ago

This package uses toJson because that is what jsonEncode expects.

But yes, you can provide your own encoder when initialising remote devtools. Something like:

final remoteDevtools = RemoteDevToolsMiddleware('192.168.1.52:8000', 
  stateEncoder: (dynamic state) => state.toMap(),
);
chirastefan commented 1 year ago
Screenshot 2023-05-06 at 23 20 49

and it doesn't seem to work

MichaelMarner commented 1 year ago

Sorry, I'm not familiar with dart mappables package so I think I gave you the wrong code snippet.

State Encoder needs to return a JSON sting. The default implementation is to use Dart's built in jsonEncode, which tries to use a toJson method on any objects it needs to encode.

As you are overriding the state encoder, you need to return a string. This actually sounds like you need to use Dart Mappables' toJson method:

final remoteDevtools = RemoteDevToolsMiddleware('192.168.1.52:8000', 
  stateEncoder: (dynamic state) => state.toJson(),
);
chirastefan commented 1 year ago

yes, but I don't think that's the issue I have with this. I'm not sure that stateEncoder is the solution for me. Before I used json_serializable for which I had to create toJson and fromJson methods that point to the generated methods from the generated file and those functions return a Map<String, dynamic>. This library dart_mappable generates toJson(returns String) and toMap(returns Map<String, dynamic>) - this is the method I need because I want to see the state obj not a long string in the dev tools. Is there a way to tell redux_dev_tools to look for toMap instead of toJson or any other solution for this issue? @MichaelMarner