美文网首页
Flutter Dio网络请求框架简单封装

Flutter Dio网络请求框架简单封装

作者: sunnytu123 | 来源:发表于2019-10-23 21:34 被阅读0次

    研究flutter有一段时间了,做了一个基于Dio简单封装

    import 'dart:io';
    
    import 'package:dio/dio.dart';
    import 'package:flutter/cupertino.dart';
    
    var NET = NetWorkManager.getInstance();
    
    enum RequestMethod {
      get ,
      post
    }
    
    class NetWorkManager {
    
         static NetWorkManager _instance;
    
         static NetWorkManager getInstance() {
           if (_instance == null) {
             _instance = NetWorkManager();
           }
           return _instance;
         }
    
         Dio dio;
         Response response;
         
    
          NetWorkManager() {
           _init();
          }
    
          _init() {
            dio = new Dio();
            dio.options.connectTimeout = 5000;
            dio.options.receiveTimeout = 3000;
          }
    
          Future request({
            @required String url ,
            RequestMethod method = RequestMethod.get,
            Map<String, dynamic> params  ,
            ResponseType responseType = ResponseType.json,
            ContentType contentType,
            Function success ,
            Function error}) async {
            try {
               response = await _baseRequest(
                   url: url ,
                   method:  method ,
                   params:  params ,
                   responseType: responseType ,
                   contentType: contentType);
    
            } on DioError catch (dioError) {
              if (error != null) {
                error(dioError);
              }
    
              return Future.error(dioError);
            }
            if (response != null) {
              if (success != null) {
                success(response);
              }
              return response;
            }
          }
    
    
    
         Future _baseRequest({
            String url,
            Map<String, dynamic> params  ,
            RequestMethod method = RequestMethod.get,
            ContentType contentType  ,
            ResponseType responseType = ResponseType.json}) async {
    
            if (contentType == null) {
              contentType =  ContentType.parse("application/x-www-form-urlencoded");
            }
    
            if (method == RequestMethod.get) {
              return await dio.get(
                  url,
                  queryParameters: params,
                  options: Options(responseType:responseType , contentType: contentType)
              );
            } else if (method == RequestMethod.post) {
              return await dio.post(
                url,
                queryParameters: params,
                options: RequestOptions(responseType: responseType , contentType: contentType)
              );
            }
          }
    
    }
    

    使用方法

     NET.request(url: "url" ,
            method: RequestMethod.post,
            contentType: ContentType.json,
            responseType: ResponseType.json,
            success: (res){
          print(res.data);
    
            } , error: (e) {
    
          }
    );
    

    或者使用Future

        NET.request(url:"url" ).then((res) {
    
        } , onError: (e) {
    
      });
    

    详细的请看demo
    喜欢的点个Star

    demo地址:
    传送门
    https://github.com/xujiyao123/NetWorkForFlutter

    相关文章

      网友评论

          本文标题:Flutter Dio网络请求框架简单封装

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