Home > Back-end >  type 'String' is not a subtype of type 'List<String>' in type cast?
type 'String' is not a subtype of type 'List<String>' in type cast?

Time:10-27

How can I fix that? I can't run the code because there is an error at String

        Text(
          questions[_questionsIndex]['questionText'] as String,
          style: TextStyle(color: Colors.orange, fontSize: 30),
        ),
        ...(questions[_questionsIndex]['questionText'] as List<String>).map((answer) {
          return Answer(_answerQuestions, answer);
        }).toList()

CodePudding user response:

The problem is this:

questions[_questionsIndex]['questionText'] as String

questions[_questionsIndex]['questionText'] as List<String>

How can questions[_questionsIndex]['questionText'] be a String and a List<String> at the same time?

I can't know for sure since you didn't show whats inside of questions variable and we don't know what you want to do but you probably want to do something like this:

...(questions as List<Map<String, dynamic>>).map((question) {
  return Answer(_answerQuestions, question['questionText']);
}).toList()

CodePudding user response:

I decided to do an online course at Udemy (flutter) and this code was shown, to load the questions & answers by a quiz:

    Question(
      questions[_questionsIndex]['questionText'],
    ),

    ...(questions[_questionsIndex]['questionText'] as List<String>).map((answer) {
      return Answer(_answerQuestions, answer);
    }).toList()

but actually it doesn't work. I tried to put 'as String' to the first Line, but the Emulator does show an error. So I tried your Version but it doesn't helped me

  • Related