import 'dart:async'; import 'dart:io'; import 'package:dio/dio.dart'; import '../config/service_url.dart'; //获得火爆专区商品的方法 Future getCategories() async => request(servicePath['getCategory']); //获得商城首页信息的方法 Future getMallGoods(String categoryId, String categorySubId, int page) async=> request(servicePath['getMallGoods'], formData: categorySubId != null ? {'categoryId': categoryId, 'categorySubId': categorySubId, 'page': page} : {'categoryId': categoryId, 'page': page}); Future request(String url, {Map formData}) async { try { Response response; Dio dio = 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; } else { throw Exception('后端接口出现异常,请检测代码和服务器情况.........'); } } catch (e) { print('ERROR: $e'); return null; } }