如何处理来自 API 请求抖动的错误

幸运的

我想处理带有一些错误消息的 http 请求抖动,并在屏幕上将其显示给用户,但我不能这样做。我只是根据建议制作它,但它对我不起作用。请任何人帮助我这是我调用API的函数。

API响应:

{
    "code": 1,
    "message": "this box is not valid !!",
    "data": [],
    "error": [],
    "status": 400
}

这是功能:

  Future<BoxModel> addBox(String boxName, String boxIdentifiant,
      String adress_circulation, String proprietaire) async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    String token = localStorage.getString('access_token');
    await checkInternet();
    Map<String, String> headers = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token'
    };
    Map<String, String> body = {
      'boxName': boxName,
      'boxIdentifiant': boxIdentifiant,
      'adress_circulation': adress_circulation,
      'proprietaire': proprietaire,
    };
    var response = await http.post(Uri.parse(ApiUtil.ADD_BOX),
        headers: headers, body: jsonEncode(body));
    if (response.statusCode == 200) {
      var body = jsonDecode(response.body);
      var data = body['message'];
      BoxModel box = BoxModel.fromJson(body);
      return box;
    } else {
      throw Exception('Failed to add box');
    }
    
  }

void addBox() async {
    if (_formKey.currentState.validate()) {
      String boxName = _boxNameController.text;
      String boxIdentifiant = _boxIdentifiantController.text;
      String adress_circulation = _adress_circulationController.text;
      String proprietaire = _proprietaireController.text;
      BoxModel box = await boxApi.addBox(
          boxName, boxIdentifiant, adress_circulation, proprietaire);
      if (box != null) {
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => BoxSettings()));
      }
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
    }
  }

我怎样才能在屏幕上显示消息错误?

赛义德·伊斯拉姆·舒沃

改变

var body = jsonDecode(response.body);
var data = body['message'];
BoxModel box = BoxModel.fromJson(body);
return box;

var body = jsonDecode(response.body);
if(body['error'].isNotEmpty || body['status'] != 200) {
    // make error boolean variable true and set in view if true show error message.
} else {
   // make error boolean variable false
   var data = body['message'];
   BoxModel box = BoxModel.fromJson(body);
   return box;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何处理来自 Node.js 中 API 的 HTTPs json 格式错误?

如何处理来自 API 的消息 - flutter

如何处理来自请求主体的图像

如何处理来自角色异常的错误

Vuex如何处理API错误通知?

如何确定REST API中的请求来自何处

如何处理来自可能包含数组或字典的API的响应?

如何处理来自外部来源的慢速Web API

使用 Geocodio API 进行批量地理编码请求时,如何处理 422 错误?

如何处理API请求中的不同响应代码?

ReactJS 异步全局 API 请求方法,如何处理响应?

如何处理来自SQL Server用户的错误?

如何处理异步/等待获取API中的错误404

如何处理Dropbox Java API中的错误

如何处理网关 API 和 Lambda 代理的错误?

使用API时如何处理不同的错误?

如何处理 Rails API 中的一般错误?

如何在 MVVM 中使用 RxSwift 正确处理来自 api 请求的错误?

如何处理来自 Rails 中 gems 的网络请求的渲染 json 错误?

如何处理fetch()中的错误请求

如何处理sql请求中的错误

如何处理API回应

API 设计:如何处理第三方的 API 错误

如何处理来自网站的 API 调用,从而显示属于该网站的信息?

如何处理来自Redux应用的api调用中的> 400个响应?

与python一起使用时,如何处理来自谷歌地图距离矩阵api的输出?

使用node.js测试API如何处理无效的JSON语法请求主体

在NodeJS / Express(和multer)附件上传实用程序中,如何处理API删除请求?

使用httparty rubygem发出api请求时如何处理分页