NetUtils.dart 714 B

1234567891011121314151617181920212223
  1. import 'dart:async';
  2. import 'package:http/http.dart' as http;
  3. class NetUtils {
  4. static Future<String> get(String url, {Map<String, String> params}) async {
  5. if (params != null && params.isNotEmpty) {
  6. StringBuffer sb = new StringBuffer("?");
  7. params.forEach((key, value) {
  8. sb.write("$key" + "=" + "$value" + "&");
  9. });
  10. String paramStr = sb.toString();
  11. paramStr = paramStr.substring(0, paramStr.length - 1);
  12. url += paramStr;
  13. }
  14. http.Response res = await http.get(url);
  15. return res.body;
  16. }
  17. static Future<String> post(String url, {Map<String, String> params}) async {
  18. http.Response res = await http.post(url, body: params);
  19. return res.body;
  20. }
  21. }