Home > Mobile >  How to post json object from array in http post request flutter?
How to post json object from array in http post request flutter?

Time:07-31

I am working on quiz I have saved the question id and the option id in array now I have to post these data in http post method in json object. I don't know how to convert array into json object here is my http.post method..

submit(testId,List<String> answer) async {
try {
  Response response = await post(
      Uri.parse(NetworkConstants.BASE_URL   'get-participate-to-test/${widget.id}'),
      headers: {
        "Authorization": "Bearer $token"
      },
      body:json.encode(
          {
        'test_id': testId,
        'question': answer,
          }
      ));
  if (response.statusCode == 200) {
    var data = jsonDecode(response.body.toString());
    print(data);
    showToast(context, data['message']);
    // Navigator.of(context).pushAndRemoveUntil(
    //     MaterialPageRoute(builder: (context) => HomeScreen()),
    //         (Route<dynamic> route) => false);
  } else {
    var data = jsonDecode(response.body.toString());
    print(data);
    showToast(context, data['message']);
  }
} catch (e) {
  setState(() {
    print(e);
  });
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Exception:"),
          content: Text(e.toString()),
          actions: [
            TextButton(
              child: Text("Try Again"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            )
          ],
        );
      });
}

} here I am sending Variable (answer) as list [{question_id: 2, option_id: 14}] how to convert it in json encode object and post in the body of the method?

CodePudding user response:

Here the answer is List of Map<String, int>. You are are already converting it to json by using json.encode in body.

submit(testId,List<Map, String> answer) async {
try {
  Response response = await post(
      Uri.parse(NetworkConstants.BASE_URL   'get-participate-to-test/${widget.id}'),
      headers: {
        "Authorization": "Bearer $token"
      },
      body:json.encode(
          {
        'test_id': testId,
        'question': answer,
          }
      ));
  if (response.statusCode == 200) {
    var data = jsonDecode(response.body.toString());
    print(data);
    showToast(context, data['message']);
    // Navigator.of(context).pushAndRemoveUntil(
    //     MaterialPageRoute(builder: (context) => HomeScreen()),
    //         (Route<dynamic> route) => false);
  } else {
    var data = jsonDecode(response.body.toString());
    print(data);
    showToast(context, data['message']);
  }
} catch (e) {
  setState(() {
    print(e);
  });
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Exception:"),
          content: Text(e.toString()),
          actions: [
            TextButton(
              child: Text("Try Again"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            )
          ],
        );
      });
}
  • Related