service_method.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import '../config/service_url.dart';
  5. //获得火爆专区商品的方法
  6. Future<Response> getCategories() async => request(servicePath['getCategory']);
  7. //获得商城首页信息的方法
  8. Future<Response> getMallGoods(String categoryId, String categorySubId, int page) async=> request(servicePath['getMallGoods'],
  9. formData:
  10. categorySubId != null ? {'categoryId': categoryId, 'categorySubId': categorySubId, 'page': page} : {'categoryId': categoryId, 'page': page});
  11. Future<Response> request(String url, {Map formData}) async {
  12. try {
  13. Response response;
  14. Dio dio = Dio();
  15. dio.options.contentType = ContentType.parse('application/x-www-form-urlencoded');
  16. if (formData == null) {
  17. response = await dio.post(url);
  18. } else {
  19. response = await dio.post(url, data: formData);
  20. }
  21. if (response.statusCode == 200) {
  22. return response;
  23. } else {
  24. throw Exception('后端接口出现异常,请检测代码和服务器情况.........');
  25. }
  26. } catch (e) {
  27. print('ERROR: $e');
  28. return null;
  29. }
  30. }