Shubham Solace
Shubham Solace

Reputation: 61

How to create JSON object in flutter?

I'm working on one application in which I want to fill the registration form and pass this data to API.I tried almost all solutions but not solved this issue.

I'm getting this type of data after filling the form

[
{Date of Birth of the Baby: 08/01/1997},
{Gender of the baby: male}, 
{Time of Birth of the Baby: 12.00 pm}, 
{Father's Name: Nnn}, 
{Mother's Name: Hbhh}, 
{Any Extra Details?: Bnn}
]

and I want this type of data

 {
    "Date of Birth of the Baby": "08/01/1997",
    "Gender of the baby": "male",
    "Time of Birth of the Baby": "12.00 pm",
    "Father's Name": "Nnn",
    "Mother's Name": "Hbhh",
    "Any Extra Details?": "Bnn"
}

var fieldsData = []; final myControllers = [];

 mydynamicData() {

    for (var i = 0; i <= widget.fieldData.length; i++) {
      fieldsData.add({
        widget.fieldData[i]['question'],
        myControllers[i].text != null || myControllers[i].text != ""
            ? myControllers[i].text
            : ""
      });

    }

    print("fieldsData:${fieldsData}");

  }

This is my method i know this issue occurred due to for loop but without for loop I'm not getting all fields data. so please help.

Upvotes: 4

Views: 19216

Answers (1)

Luiz Filipe Medeira
Luiz Filipe Medeira

Reputation: 1320

Import:

import 'dart:convert';

then you can use:

json.encode(fieldsData);

As you have a list of objects, it may be necessary to make a for in the list calling json.encode for each item, then encoding the result.

Upvotes: 7

Related Questions