我正在尝试在 Flutter 应用程序中使用 Bloc 包进行状态管理。我有一种情况,我在同一个屏幕上有两个列表,我很困惑应该为每个列表使用两个肘,或者我可以在下面的代码中制作类似的东西,我对两个列表都使用一个肘。我想知道这种构造代码的方式是否正确?在这张图片中是屏幕的草图。
abstract class CounterState extends Equatable {
const CounterState({this.valOne, this.valTwo, this.error});
final int? valOne;
final int? valTwo;
final String? error;
@override
List<Object?> get props => [valOne, valTwo, error];
}
class CounterInitial extends CounterState {}
class ValOneSuccess extends CounterState {
const ValOneSuccess(int? val, int? valTwo)
: super(valOne: val, valTwo: valTwo);
}
class ErrorState extends CounterState {
const ErrorState(int? val, int? valTwo, String error)
: super(valOne: val, valTwo: valTwo, error: error);
}
class CounterCubit extends Cubit<CounterState> {
CounterCubit() : super(CounterInitial());
void loadData() async {
int valOne;
int valTwo;
emit(CounterInitial());
try {
valOne = 2;
emit(ValOneSuccess(valOne, state.valTwo));
///If error is thrown
//throw Exception('ValueOne exception');
} catch (e) {
print(e);
emit(ErrorState(state.valOne, state.valTwo, e.toString()));
}
try {
valTwo = 1;
emit(ValOneSuccess(state.valOne, valTwo));
///If error is thrown
//throw Exception('ValueTwo exception');
} catch (e) {
state.error != null
? emit(ErrorState(
state.valOne, state.valTwo, 'Exception on both values'))
: emit(ErrorState(state.valOne, state.valTwo, e.toString()));
}
}
}