Home > Blockchain >  How to push array in post method Flutter
How to push array in post method Flutter

Time:10-10

I have created a post method and I need to push students id in body parameter. My api expect this formatted data:

[date] => 2021-09-24
[time] => 11:27 AM
[no_of_attendance] => 1
[ids] => Array
    (
         [19243] => 19243
         [19250] => 19250
         [19259] => 19259
         [19260] => 19260
         [19316] => 19316
      )

[save_as] => draft

When I am sending data my log is showing this values:

Data: {date: 2021-09-26, time: 03:18 PM, no_of_attendance: 1, ids: [18047, 18093, 18289, 18047, 18093], save_as: draft}

I am collecting student's id via SwitchListTile where I am adding student id in my List variable.

How can I post data to my api?

My Post method:

static Future<Map<String, dynamic>> sendDraftOrFinalData(
      departmentId, batchId, semester, courseId, token, Map data) async {
    final url =
        Api.baseUrl   'attendance_students/$departmentId/$batchId/$semester/$courseId?token=$token';

    print('Data: $data');

    final response = await client.post(Uri.parse(url), body: data);

    final responseBody = json.decode(response.body);

    return responseBody;
  }

My controller where I am calling my post method:

void saveAsDraftOrFinal(departmentId, batchId, semester, courseId, saveAs) async {
    try {
      if (dateSelectError.value) {
        isLoading.value = true;
        await RemoteServices.sendDraftOrFinalData(
            departmentId, batchId, semester, courseId, token, {
          'date': selectedDate.value,
          'time': selectedTime.value,
          'no_of_attendance': selectedAttendanceTime.value.toString(),
          'ids': selectedStudentsIds.toString(), // This is my student ids list
          'save_as': saveAs,
        }).then((value) {
          print('Attendance Response: $value');
    } finally {
      isLoading.value = false;
    }
  }

When I am submitting data, I am getting this error:

E/flutter (21836): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (21836): <!DOCTYPE html>
E/flutter (21836): ^
E/flutter (21836): 
E/flutter (21836): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter (21836): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1272:9)
E/flutter (21836): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:937:22)
E/flutter (21836): #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter (21836): #4      JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter (21836): #5      JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter (21836): #6      RemoteServices.sendDraftOrFinalData (package:diu_qr_code_scanner/services/remote_services.dart:94:31)
E/flutter (21836): <asynchronous suspension>
E/flutter (21836): #7      AttendanceController.saveAsDraftOrFinal (package:diu_qr_code_scanner/controllers/attendance_controller.dart:65:9)
E/flutter (21836): <asynchronous suspension>

CodePudding user response:

I got it by updating my controller method:

void saveAsDraftOrFinal(departmentId, batchId, semester, courseId, saveAs) async {
    try {
      if (dateSelectError.value) {
        isLoading.value = true;
        await RemoteServices.sendDraftOrFinalData(
            departmentId, batchId, semester, courseId, token, {
          'date': selectedDate.value,
          'time': selectedTime.value,
          'no_of_attendance': selectedAttendanceTime.value.toString(),
          for (var id in selectedStudentsIds)
            'ids[${id.toString()}]': id.toString(),
          'save_as': saveAs,
        }).then((value) {
          print('Attendance Response: $value');
    } finally {
      isLoading.value = false;
    }
  }

Then my post data is like below:

Data: {date: 2021-09-26, time: 05:56 PM, no_of_attendance: 1, save_as: final, ids[19243]: 19243, ids[19250]: 19250}
  • Related