美文网首页
Flutter 基于dio网络请求封装

Flutter 基于dio网络请求封装

作者: DavidATM | 来源:发表于2019-06-19 18:21 被阅读0次

    import 'package:dio/dio.dart';

    import 'dart:convert';

    class BaseRequest {

    /*分页默认起始页*/

      static final intkStartIndex =1;

      /*分页默认每一页数量*/

      static final intkPageSize =10;

      /*请求成功*/

      static final intkCodeSuccess =0;

      /*请求地址*/

      static final StringkHttpHostName ="http://127.0.0.1:8080";

      /*登录*/

      static final StringkUrlAccountLogin =kHttpHostName +"/user/login";

      static requestWithUrl(String url, Map parameters, successBlock(int code, String message, Object data), errorBlock(error))async {

    try {

    Dio dio =new Dio();

          Response response =await dio.request(

    url,

              queryParameters: parameters,

              options:Options(

    method:"POST"

              )

    );

          print("请求地址:" + url);

          print("请求参数:" + parameters.toString());

          print("返回参数:" + response.toString());

          Map map = json.decode(response.toString());

          int code = map["code"];

          String msg = map["msg"];

          Object data = map["data"];

          return successBlock(code,msg,data);

        }on DioErrorcatch (error) {

    print("请求地址:" + url);

          print("请求参数:" + parameters.toString());

          print("请求报错:" + error.toString());

          return errorBlock(error);

        }

    }

    }

    调用:

    BaseRequest.requestWithUrl(BaseRequest.kUrlAccountLogin, {

    "account":"18761661234",

      "password":"111111"

    }, (int code, String message, Object data) {

    if (code == BaseRequest.kCodeSuccess) {

      }else {

      }

    }, (error) {

    });

    相关文章

      网友评论

          本文标题:Flutter 基于dio网络请求封装

          本文链接:https://www.haomeiwen.com/subject/lnclqctx.html