为什么Future总是为HTTP请求返回错误?

时间:2020-11-06 09:51:09

标签: api flutter http dart async-await

当我上次打开时,它工作得很好。但是现在它总是抛出错误。

import 'dart:convert';
import 'package:easy_shop/main.dart';
import 'package:http/http.dart' as http;
import '../models/api_response.dart';
import '../models/group.dart';
class GroupService {
  String url = MyApp.BASE_URL;
  Future<APIResponse<List<Group>>> getGroupList() {
    return http.get(url + '/api/group?&pagenumber=0&pagesize=20').then((data) {
      print('Status code : ${data.statusCode}');
      if (data.statusCode == 200) {
        final Iterable jsonData = json.decode(data.body);
        final List<Group> groups =
            jsonData.map((e) => Group.fromJson(e)).toList();
        return APIResponse<List<Group>>(data: groups);
      }
      return APIResponse<List<Group>>(
          error: true, errorMessage: 'An error occured');
    }).catchError((_) => APIResponse<List<Group>>(
        error: true, errorMessage: 'An error occured'));
  }
}

0 个答案:

没有答案