Отправка данных Flutter по запросу на размещение - PullRequest
0 голосов
/ 08 мая 2020

Мне нужно поместить некоторые данные через api, вот мой код

  static voteplusQuestion(data, int flag){
    print('Api');
    print(data);
    if( flag == 1 ){
      var updateQuestionData = {'Would': data.would, 'Rather': data.rather, 'wouldClick': data.wouldClick, 'ratherClick': data.ratherClick + 1, 'QuestionID': data.QuestionID,};
      print(updateQuestionData);
    }
    if( flag == 0){
      var updateQuestionData = {'Would': data.would, 'Rather': data.rather, 'wouldClick': data.wouldClick  + 1, 'ratherClick': data.ratherClick, 'QuestionID': data.QuestionID,};
      print(updateQuestionData);
    }
    return http.put(
      'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather',
      headers: <String, String>{
        'Content-Type': 'application/json',
      },

    );
  }

Мне нужно поместить updateQuestionData в API, но я не уверен, как я могу это сделать. Также вывод на печать updateQuestionData выглядит следующим образом

{Would: Shaving Cream, Rather: Soap, wouldClick: 15, ratherClick: 13, QuestionID: 16563fa7-e833-445f-a76b-a9fbaab3a301}

И Api работает в кодовой ручке, как это

var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather');
xhr.onreadystatechange = function(event) {
  console.log(event);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');

xhr.send(JSON.stringify({Would: "Coffe",Rather: "Tea", wouldClick: 15, ratherClick: 13, QuestionID: "16563fa7-e833-445f-a76b-a9fbaab3a301"}));

Я установил заголовок в функции, но теперь не знаю, как поставил тело

1 Ответ

0 голосов
/ 08 мая 2020

Вы можете отправить данные с помощью метода put следующим образом:

 static voteplusQuestion(data, int flag) async{
   var updateQuestionData;

   if( flag == 1 ){
      updateQuestionData = {'Would': data.would, 'Rather': 
      data.rather, 'wouldClick': data.wouldClick, 'ratherClick': 
      data.ratherClick + 1, 'QuestionID': data.QuestionID,};
   }

  if( flag == 0){
   updateQuestionData = {'Would': data.would, 'Rather': data.rather, 
    'wouldClick': data.wouldClick  + 1, 'ratherClick': 
    data.ratherClick, 'QuestionID': data.QuestionID,};

   }

    String url = 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather';
    Map<String, String> headers = {"Content-type": "application/json"};
    String data = jsonEncode(updateQuestionData);
    // make PUT request
    Response response = await put(url, headers: headers, body: data);
    // check the status code for the result
    int statusCode = response.statusCode;
    print(statusCode);
    // this API passes back the updated item with the id added
    String body = response.body;
    print(body);

   return body;
 }

Я не уверен насчет вашего варианта использования, но считаю, что это можно было бы реализовать намного чище.

Примечание: чтобы использовать метод put или выполнить операцию update, вам необходимо добавить id элемента, я уверен, что url существовал в этом примере

...