大家好,我有一个屏幕,打开时,一个请求飞到服务器,它返回400错误状态给我,但为什么它没有进入错误状态,而是进入异常,为什么?
当块中的状态不是200时(状态是OrderFinishError),我应该点击,但是我点击了throw StateError('err');
以下是我的州-
abstract class OrderFinishState {}
class OrderFinishNotLoading extends OrderFinishState {}
class OrderFinishLoading extends OrderFinishState {}
class OrderFinishLoaded extends OrderFinishState {
OrderFinishLoaded({required this.orderFinishModels});
final OrderFinishModels orderFinishModels;
}
class OrderFinishError extends OrderFinishState {
OrderFinishError({required this.errorModels});
final ErrorModels errorModels;
}
我的孩子-
class OrderFinishCubit extends Cubit<OrderFinishState> {
OrderFinishCubit() : super(OrderFinishNotLoading());
Future<void> orderFinish() async {
emit(OrderFinishLoading());
final prefs = await SharedPreferences.getInstance();
final token = prefs.getString('token');
final orderId = prefs.getInt('orderId');
final response = await http.post(Uri.parse(ConfigUrl.orderFinish),
headers: {
"Content-Type": "application/vnd.api+json",
"Accept": "application/vnd.api+json",
"Authorization": "Bearer $token",
},
body: jsonEncode(<String, dynamic>{
"order_id": orderId,
}));
if (response.statusCode == 200) {
log('test order finish cubit');
log(orderId.toString());
final responseJson = json.decode(response.body) as Map<String, dynamic>;
log(json.decode(response.body).toString());
emit(OrderFinishLoaded(
orderFinishModels: OrderFinishModels.fromJson(responseJson)));
} else {
final jsonError = json.decode(response.body) as Map<String, dynamic>;
log(response.statusCode.toString());
log(json.decode(response.body).toString());
emit(OrderFinishError(errorModels: ErrorModels.fromJson(jsonError)));
}
}
}
我的BlocBuilder -
return BlocProvider<OrderFinishCubit>(
create: (context) => OrderFinishCubit()..orderFinish(),
child: Scaffold(
body: BlocBuilder<OrderFinishCubit, OrderFinishState>(
builder: (context, state) {
if (state is OrderFinishNotLoading) {
return Center(
child:
Text('Дождитесь загрузки или перезапустите приложение'),
);
} else if (state is OrderFinishLoading) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (state is OrderFinishLoaded) {
return Home();
} else if (state is OrderFinishError) {
Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(state.errorModels.message),
SizedBox(
height: 15,
),
InkWell(
onTap: () async {
context.go('/activeTravel');
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(32),
color: ConfigColor.green,
),
width: width,
height: 44,
child: Center(
child: Text('На главную',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w500)),
))),
],
),
));
}
throw StateError('err');
},
),
));
请注意,我有一个类似的代码,只是在不同的屏幕上,它工作得很好。
请帮帮我,这是项目的最后一项任务))
1条答案
按热度按时间af7jpaap1#
这是因为你不是
returning
小工具。变更
到