Jax
Jax

Reputation: 3

type 'String' is not a subtype of type 'List<String>' in type cast?

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()

Upvotes: 0

Views: 92

Answers (2)

pedro pimont
pedro pimont

Reputation: 3084

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()

Upvotes: 1

Jax
Jax

Reputation: 3

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

Upvotes: 0

Related Questions