I want to send the scan data results in the form of arguments to the homescreen page, and then call pinch getUnit(param). I got a true response from the api that I got. But the data is not displayed because of an error. Bad state: Cannot emit new states after calling close.
Error :
Cubit :
Future<void> getUnit({String? name}) async{
emit(UnitFetch.loading());
try {
final res = await _meterService.getUnit(kode_unit: name);
emit(UnitFetch.success(data: res));
} catch (e, trace) {
print(e);
print(trace);
emit(UnitFetch.error(msg: 'Gagal'));
}
}
State
class UnitFetch extends MeterState {
final LoadStatus? load;
final String? message;
final UnitListDataModel? data;
UnitFetch({this.load, this.message, this.data});
UnitFetch.loading({
String? msg,
}) : this(load: LoadStatus.loading, message: msg);
UnitFetch.success({
String? msg,
UnitListDataModel? data,
}) : this(load: LoadStatus.success, message: msg, data: data);
UnitFetch.error({required String msg})
: this(load: LoadStatus.error, message: msg);
}
HomeScreen get data with argument
void _fetch() {
if (qrText != '') {
context.read<MeterCubit>().getUnit(name: qrText);
} else {
context.read<MeterCubit>().getUnitList();
}
}
HomeScreen :
BlocBuilder<MeterCubit, MeterState>(
buildWhen: (previouse, current) => current is UnitFetch,
builder: (context, state) {
if (state is UnitFetch) {
_lastMeter = state.data?.endMeter;
if (state.load == LoadStatus.loading) {
return Center(
child: CircularProgressIndicator(
color: ColorData.primary,
),
);
} else if (state.data == null) {
return Text('Data Not Found');
}
return InputTextField(
title: "Kode Unit",
hint: "${state.data?.kodeUnit ?? ''}",
widget: SizedBox(),
controller: _lastMeterController,
);
}
return InputTextField(
title: "Kode Unit",
hint: "${_lastMeter ?? ''}",
widget: SizedBox(),
controller: _lastMeterController,
);
},
),