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

Asynchronous fromJson and toJson #1420

Closed
felangel opened this issue Jul 9, 2020 · 2 comments
Closed

Asynchronous fromJson and toJson #1420

felangel opened this issue Jul 9, 2020 · 2 comments
Labels
pkg:hydrated_bloc This issue is related to the hydrated_bloc package question Further information is requested wontfix This will not be worked on
Projects

Comments

@felangel
Copy link
Owner

felangel commented Jul 9, 2020

@saivarunr:

Greetings @felangel , hope you are healthy and safe. I would also like to thank you for the libraries you have provided to the community. I'm using bloc and hydrated bloc. I was thinking why not provide async support for both fromJson and toJson on the HydratedBloc (

hydrated_bloc/lib/src/hydrated_bloc.dart

Line 37 in 15eb68f

return _state = fromJson(Map<String, dynamic>.from(stateJson));
)
Describe the solution you'd like
I have this use case where we have to perform compute, validate checksum and then consider the input from local cache for further processing. At first, glance looks like it is not possible to invoke async methods/functions inside fromJson. If it cannot be implemented due to the underlying API, how do you suggest I should handle this problem on my side?

@felangel felangel added question Further information is requested pkg:hydrated_bloc This issue is related to the hydrated_bloc package labels Jul 9, 2020
@felangel felangel added this to To do in bloc via automation Jul 9, 2020
@felangel
Copy link
Owner Author

felangel commented Aug 3, 2020

Closing this for now because to/from json must be synchronous based on the current API specification.

@felangel felangel closed this as completed Aug 3, 2020
bloc automation moved this from To do to Done Aug 3, 2020
@felangel felangel added the wontfix This will not be worked on label Aug 3, 2020
@inveker
Copy link

inveker commented Oct 15, 2022

If you are satisfied that the initial state will be the default, and the restored state will be set via an event, you can use this code

 @override
 State? fromJson(Map<String, dynamic> json) {
   Future(() => add(RestoreEvent(json)));
 }
  
 Future<void> _restore(RestoreEvent event, Emitter<State> emit) async {
   final json = event.json;
   emit(State(...));
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:hydrated_bloc This issue is related to the hydrated_bloc package question Further information is requested wontfix This will not be worked on
Projects
bloc
  
Done
Development

No branches or pull requests

2 participants