Access to a element of array value in json from dart

Issue

I have this const variable in flutter:

static const questions = [
    {
      "question": "question",
      "answers": ["1", "2"],
      "message":
          "message",
    }, 
]

how to access to "1" from dart??

I try using QuestionContent.questions[0]["answers"][0].
But I got error "The method '[]' can't be unconditionally invoked because the receiver can be 'null'"

Solution

Try use:

Text((QuestionContent.questions[0] as Map<String, dynamic>)['answers'][0] ?? '')

or:

Text((questions[0]['answers'] as List<String>)[0]),

Answered By – Saitoh Akira

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published