4

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.

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

[ {"Date of Birth of the Baby":"08/01/1996"}, {"Gender of the baby":""}, {"Time of Birth of the Baby":"12.99pm"}, {"Father's Name":"Nnn"}, {"Mother's Name":"Ggg"}, {"Any Extra Details?":"Bhhh"} ] it gives this type of data but I don't want curly braces like I want only [{ "" : "" }]
please let me know if you have any idea
you have a list of objects, you need to convert your list to a map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.