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());
});
}