flutter05: 인터페이스 함수 봉인 요청

1323 단어
플러그인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');
 });

좋은 웹페이지 즐겨찾기