Skip to content

fix(hydrated_bloc): persist typed lists whose first element is atomic - #4952

Open
Yasser-Ameur wants to merge 1 commit into
felangel:masterfrom
Yasser-Ameur:fix/hydrated-bloc-typed-list
Open

Yasser-Ameur wants to merge 1 commit into
felangel:masterfrom
Yasser-Ameur:fix/hydrated-bloc-typed-list

Conversation

@Yasser-Ameur

Copy link
Copy Markdown

Status

READY

Breaking Changes

NO

Description

Fixes #4951.

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.

In _traverseComplexJson the copy of the list was chosen from the first element only. When that element was atomic the copy was object.sublist(0), which keeps the static element type, so the converted Map of 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 restores List<String>, List<int>, List<double> and List<bool> typed after a restart, so a fromJson that casts json['x'] as List<String> (as the ListCubit in 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 as List<dynamic>. The JSON is the same.

Tests: two e2e cases in e2e_test.dart (a nullable list with a leading null persists and restores, and a non-nullable control). The first fails on master with the error above. dart test passes (124), coverage of lib stays at 100 percent, dart format and dart analyze --fatal-infos --fatal-warnings . are clean.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

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

No deployments
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

Successfully merging this pull request may close these issues.

fix: hydrated_bloc throws when a typed list starts with null and holds an object later

1 participant