3

What I am trying to do is creating a variable that can hold the following data:

questiion = { 'title': '',
            'description': '',
            'questionTitle': '',
            'option1Text': '',
            'option2Text': '',
            'option3Text': '',
            'option1Correctness': false,
            'option2Correctness': false,
            'option3Correctness': false,
            'questionNumber' : 1 }

Then I can get these values from forms and save the users input data into them like following:

          onSaved: (value) {
            question['title'] = value!;
          },

I don't know if there is any data type in flutter for doing that? I know there is Map<> but it can only consist 1 pair of key:value. Maybe I should create a nested Map? Or there is a better way?

2
  • 1
    This questions variable is already a Map<String, dynamic> and can do what it is you're asking. A Map can take very many pair values. Commented Mar 4, 2023 at 18:54
  • @DARTender: Yeah, I didn't know a dynamic variable can hold a JSON string. Commented Mar 4, 2023 at 19:02

2 Answers 2

5

Using Map<String,dynamic> should be right way (if you don't want to create a class).

Like this:


    final question = <String, dynamic>{
      'title': '',
      'description': '',
      'questionTitle': '',
      'option1Text': '',
      'option2Text': '',
      'option3Text': '',
      'option1Correctness': false,
      'option2Correctness': false,
      'option3Correctness': false,
      'questionNumber': 1
    };

    print(question);

The print will give you this output:

{title: , description: , questionTitle: , option1Text: , option2Text: , option3Text: , option1Correctness: false, option2Correctness: false, option3Correctness: false, questionNumber: 1}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can go for a nested key-value map, or you can also keep everything in an object.

This ans might help you: Json parsing in dart (flutter)

Comments

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.