Hello everyone,
I have the following scenario that I'm struggling with and I'm looking for help on how to approach it.
Context
I have an entity that contains a list of objects, each object contains an id and a text. I want to show the list of objects in a ListView
where each item has a TextField
showing the object's text property. This means an Output
containing the list of objects is needed:
class SomeEntity extends Entity {
final List<SomeObject> someObjects;
SomeEntity(this.someObjects);
@override
List<Object?> get props => [someObjects];
}
class SomeObject {
final String someText;
final String id;
SomeObject(this.someText, this.id);
}
class SomeUseCase extends UseCase<SomeEntity> {
SomeUseCase()
: super(
entity: SomeEntity([]),
outputFilters: {
SomeListViewOutput: (SomeEntity someEntity) {
return SomeListViewOutput(
someObjects: someEntity.someObjects,
);
},
},
);
}
class SomeListViewOutput extends Output {
final List<SomeObject> someObjects;
SomeObjectListViewOutput({
required this.someObjects,
});
@override
List<Object?> get props => [someObjects];
}
When the user makes any change to the text in the TextField
, the entity needs to be updated.
Problem
When the user makes any change to the text in the TextField
and the entity gets updated, the ListView
and all of its items are being rebuilt although only the item that has changed text should be rebuilt.
It's not possible to make an Output
that is generated at run time based on parameters since the Riverpod provider is being created internally and using the .family
modifier is not possible. If that was possible, then such a generator could receive the id of the object and return an Output
that can be used by the individual items in the ListView
and the ListView
would use an Output
that only contains the ids of the objects so that It doesn't get rebuilt every time an object changes:
class SomeListViewOutput extends Output {
final List<String> ids;
SomeListViewOutput({
required this.ids,
});
@override
List<Object?> get props => [ids];
}
class SomeListViewItemOutput extends Output {
final String someText;
SomeListViewItemOutput({
required this.someText,
});
@override
List<Object?> get props => [someText];
}
Do you have an idea how to approach this problem?