service_method.dart 981 B

1234567891011121314151617181920212223242526272829303132
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import '../config/service_url.dart';
  5. Future<Response> getCategories() => request(servicePath['getCategory']);
  6. Future<Response> getMallGoods(String categoryId, String categorySubId, int page) => request(servicePath['getMallGoods'],
  7. formData:
  8. categorySubId != null ? {'categoryId': categoryId, 'categorySubId': categorySubId, 'page': page} : {'categoryId': categoryId, 'page': page});
  9. Future<Response> request(String url, {Map formData}) async {
  10. try {
  11. Response response;
  12. Dio dio = Dio();
  13. dio.options.contentType = ContentType.parse('application/x-www-form-urlencoded');
  14. if (formData == null) {
  15. response = await dio.post(url);
  16. } else {
  17. response = await dio.post(url, data: formData);
  18. }
  19. if (response.statusCode == 200) {
  20. return response;
  21. } else {
  22. throw Exception('Net Error');
  23. }
  24. } catch (e) {
  25. print('ERROR: $e');
  26. return null;
  27. }
  28. }