美文网首页
flutter05: 请求接口函数封装

flutter05: 请求接口函数封装

作者: 蜗牛的学习方法 | 来源:发表于2019-06-17 14:38 被阅读0次

下载插件 dio: ^2.1.7

import 'package:dio/dio.dart';
import 'dart:async';
import 'dart:io';
import './service.dart';

//POST 请求
Future post(type,data) async{
  try{
    Response response;
    Dio dio=new Dio();
    dio.interceptors.add(InterceptorsWrapper(
      onRequest:(Options options) async{
        options.headers["token"] = token;
        return options; //continue
      }
    ));
    response=await dio.post(service[type],data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception('后端接口出现异常');
    }
  }catch(e){
    return print('error:$e');
  }
}

//GET 请求
Future get(type,data) async{
  try{
    Response response;
    Dio dio=new Dio();
    dio.interceptors.add(InterceptorsWrapper(
      onRequest:(Options options) async{
        options.headers["token"] = token;
        return options; //continue
      }
    ));
    response=await dio.post(service[type],queryParameters: data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception('后端接口出现异常');
    }
  }catch(e){
    return print('error:$e');
  }
}

使用:

get('systemConfig', {}).then((res){
    print('@@@$res');
 });

相关文章

  • flutter05: 请求接口函数封装

    下载插件 dio: ^2.1.7 使用:

  • 接口处理封装请求模块

    对接口后端的接口文档之后 封装请求模块 项目中我们需要通过接口进行数据请求,为了便于操作,要封装用于请求操作的函数...

  • ajax请求封装

    能发起ajax请求的函数模块 函数的返回值是promise对象 进行接口请求封装

  • 2020-12-16

    学习圈up 接口文档 请求头封装:带token封装请求头: getRequestHeader: function ...

  • springcloud ribbon 的简单使用

    RestTemplate 对http请求通信的封装,封装了http请求,方便的请求http接口。 Ribbon r...

  • nui-app里面的请求接口简单的封装

    nui-app里面的请求接口简单的封装 新建一个封装接口的文件 挂在到Vue原型上 要请求接口的页面

  • 请求接口封装

    import axios from 'axios'import { Message, MessageBox, Lo...

  • Kotlin中协程封装与取消

    1. 将java中的回调接口封装成挂起函数 以OKHttp请求为例,代码如下 : 挂起函数的使用如下: 因为Cal...

  • axios封装

    1、封装axios 2、对请求做封装,具体怎么来封装,根据自己的接口来 3、接口 4、调用

  • 小程序请求API接口,网络请求封装

    概述 前几日关注的用户问我有没有封装好的小程序网络请求接口,那么小程序请求API接口,网络请求封装,我们应该怎么编...

网友评论

      本文标题:flutter05: 请求接口函数封装

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