flutter之dio网络通信

flutter http请求库dio:

(1)在pubspec.yam中添加:

dependencies:
  flutter:
    sdk: flutter


  dio: ^2.0.7

 (2)网络工具类post:

       

Future request(url,{formData})async{
    try{
      Response response;
      Dio dio = new Dio();
      dio.options.contentType=ContentType.parse("application/x-www-form-urlencoded");
      if(formData==null){
        
          response = await dio.post(url);
      }else{
          response = await dio.post(url,data:formData);
      }
      if(response.statusCode==200){
        return response.data;
      }else{
          throw Exception('后端接口出现异常,请检测代码和服务器情况.........');
      }
    }catch(e){
        return print('ERROR:======>${e}');
    }
     
}

(3)运用方法:

   //得到商品列表数据
   void _getGoodLists(context,String categorySubId) {
     
    var data={
      'categoryId':"1234",
      'categorySubId':categorySubId,
      'page':10
    };
    
    request(url,formData:data ).then((val){
       //后台的数据
        var  data = json.decode(val.toString());
         
      
    });
  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章