Flutter圈子FlutterFlutter

Flutter Dio网络请求封装(GET,POST)

2019-08-13  本文已影响38人  calary

一、前言

最近学习Flutter进行到了网络这一块,所以就花了点时间对网络请求进行了简单的封装,以便使用,网络请求基于Dio,具体使用可自行学习。

二、结构

image.png

三、代码

import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:demo/model/base_model.dart';
enum Method {
  get,
  post
}

class Net{
  // 工厂模式
  factory Net() => _getInstance();
  static Net get instance => _getInstance();
  static Net _instance;

  Dio dio;
  Net._internal() {
    // 初始化
    dio = Dio(BaseOptions(
      connectTimeout: 60000,   // 连接服务器超时时间,单位是毫秒.
      receiveTimeout: 10000,   // 响应流上前后两次接受到数据的间隔,单位为毫秒, 这并不是接收数据的总时限.
      )
    );
  }
  // 单列模式
  static Net _getInstance() {
    if (_instance == null){
      _instance = Net._internal();
    }
    return _instance;
  }

  get(String url, Map<String,dynamic> params, {Function success,Function failure}){
    _doRequest(url, params, Method.get, success, failure);
  }

  post(String url, Map<String,dynamic> params, {Function success,Function failure}){
    _doRequest(url, params, Method.post, success, failure);
  }

  void _doRequest(String url, Map<String,dynamic> params ,Method method, Function successCallBack, Function failureCallBack) async{
      try{
        Response response;
        switch (method){
          case Method.get:
            if (params != null && params.isNotEmpty){
              response = await dio.get(url,queryParameters: params);
            }else {
              response = await dio.get(url);
            }
            break;
          case Method.post:
            if (params != null && params.isNotEmpty){
              response = await dio.post(url,queryParameters: params);
            }else {
              response = await dio.post(url);
            }
            break;
        }
        Map<String, dynamic> result = json.decode(response.toString());
        // 打印信息
        print('''api: $url\nparams: $params\nresult: $result''');
       // 转化为model
        BaseModel model = BaseModel.fromJson(result);
        if (model.code == 200){ // 200 请求成功
          if (successCallBack != null){//返回请求数据
            successCallBack(model.data);
          }
        }else {
          //TODO
          //直接使用Toast弹出错误信息
          //返回失败信息
           if (failureCallBack != null){
             failureCallBack(model.error);
           }
        }
      }catch (exception){
        print('错误:${exception.toString()}');
        Fluttertoast.showToast(msg: "请求失败,请稍后再试");
        if (failureCallBack != null){
          failureCallBack(exception.toString());
        }
      }
  }
}
class BaseModel {
  int code;
  dynamic data;
  String error;

  BaseModel({this.code, this.data, this.error});

  BaseModel.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    data = json['data'];
    error = json['error'];
  }
}
class NetUrl {
  /// 服务器地址
  static const String BASE_URL = "http://www.dio.com";
  /// 登录接口
  static const String LOGIN_USER  = BASE_URL + '/login';
}
import "net.dart";
import 'net_url.dart';
class NetUtil {
  // 登录
   static void login(Map<String,dynamic> params,{Function success, Function failure}){
      Net().post(NetUrl.LOGIN_USER, params,success: success, failure: failure);
   }
}

4、具体使用

 void requestData(){
    var params = {'username':'xxx','password':'xxxx'};
    NetUtil.login(params,success: (response){

    },failure: (error){
      
    });
  }

5、总结

以上封装还有几点待完善

上一篇下一篇

猜你喜欢

热点阅读