Tho*_*mas 16 architecture flutter bloc cubit
使用 BLoC 库时,我们将所有变量存储在状态类中。但是在哪里存储 TextEditingController,它不会改变,但它的值会改变?
假设我有一个像这样的状态类(仅作为示例):
@freezed
abstract class EditItemState with _$EditItemState {
const factory EditItemState.updated({
TextEditingController titleController,
ShoppingItem shoppingItem,
}) = _ShoppingListLoaded;
}
Run Code Online (Sandbox Code Playgroud)
和肘级:
class EditItemCubit extends Cubit<EditItemState> {
EditItemCubit() : super(EditItemState.updated());
Future<void> titleUpdated() async {
emit(
EditItemState.updated().copyWith(
shoppingItem: state.shoppingItem.copyWith(
title: state.titleController.text,
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
所以Cubit类逻辑看起来很混乱。我建议将此类控制器直接保留在小部件或 BLoC/Cubit 类中。这是正确的做法吗?