Repository navigation
fix(hydrated_bloc): persist typed lists whose first element is atomic - #4952
Open
Yasser-Ameur wants to merge 1 commit into
Open
Yasser-Ameur wants to merge 1 commit into
Yasser-Ameur wants to merge 1 commit into
Conversation
Emitting a `List<Item?>` such as `[null, Item]` threw `type '_Map<String, dynamic>' is not a subtype of type 'Item?'` out of `emit`, while `[Item, null]` persisted. This is the shape json_serializable produces with the default `explicitToJson: false`. When the first element was atomic, the traversal copied the list with `sublist(0)`, which keeps the static element type, so a later element converted by `toJson` could not be stored in it. Keep that copy for all-atomic lists, and widen it to `List<dynamic>` once an element needs converting.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status
READY
Breaking Changes
NO
Description
Fixes #4951.
Emitting a
List<Item?>such as[null, Item()]threwtype '_Map<String, dynamic>' is not a subtype of type 'Item?'out ofemit, while[Item(), null]persisted. This is the shapejson_serializableproduces with the defaultexplicitToJson: false.In
_traverseComplexJsonthe copy of the list was chosen from the first element only. When that element was atomic the copy wasobject.sublist(0), which keeps the static element type, so the convertedMapof a later element could not be stored in it.The copy now stays typed while every element is atomic and is widened to
List<dynamic>once an element needs converting. I kept the typed copy for all-atomic lists on purpose: Hive restoresList<String>,List<int>,List<double>andList<bool>typed after a restart, so afromJsonthat castsjson['x'] as List<String>(as theListCubitin the tests does) keeps working in the same process and after a restart.One visible difference: a mixed list that starts with an atom, for example a
List<Object?>holding[1, Item()], is now handed to storage asList<dynamic>. The JSON is the same.Tests: two e2e cases in
e2e_test.dart(a nullable list with a leadingnullpersists and restores, and a non-nullable control). The first fails on master with the error above.dart testpasses (124), coverage oflibstays at 100 percent,dart formatanddart analyze --fatal-infos --fatal-warnings .are clean.Type of Change