首页 > 解决方案 > Flutter:当我尝试在 post requestFl 的正文中将 int 转换为 json 时出错

问题描述

 Future<void> createReservation(String fromTime, String toTime, String areaId,
      int numberOfPerson, String date) async {
    Map<String, dynamic> data = Map<String, dynamic>();
    data['area_id'] = areaId;
    data['number_of_people'] = numberOfPerson;
    data['from_time'] = date + " " + fromTime;
    data['to_time'] = date + " " + toTime;
    final prefs = await SharedPreferences.getInstance();
    if (prefs.containsKey(Constant.prefsUserAccessTokenKey)) {
      token = prefs.getString(Constant.prefsUserAccessTokenKey);
    }
    final url = Urls.create_reservasion;
    try {
      final response = await retry(
          () => http
              .post(url,
                  headers: 
                    'Authorization': 'Oauth2 ' + token
                  },
                  body: data)
              .timeout(Duration(seconds: 5)),
          retryIf: (e) => e is TimeoutException || e is SocketException);
      print('create reservation is:');
      print(response.body);
      if (response.statusCode == 200) {
      } else if (response.statusCode == 401) {
        throw HttpException('401');
      } else if (response.statusCode == 500) {
        print('server error');
        print(response.statusCode);
        throw HttpException("Something went wrong");
      } else {
        print(json.decode(response.body)['detail']);
        print(response.statusCode);
        throw HttpException(json.decode(response.body)['detail']);
      }
    } catch (error) {
      throw error;
    }
  }

{

"area_id":"282b1837-c052-4ae7-89e5-171787599362",

"number_of_people":1,

"from_time":"2020-09-22 14:00:00",

"to_time":"2020-09-22 14:30:00"

}

标签: jsonflutterhttpdartprovider

解决方案


推荐阅读