Home > Software design >  the argument type 'object' cant be assigned to parameter type String
the argument type 'object' cant be assigned to parameter type String

Time:11-10

Learning work with Maps...Can't have access to the element specified? what is the problem. please give me some links to read or watch working with Maps in flutter. enter image description here

CodePudding user response:

[![enter image description here][1]][1]

here is the question file which i use it in column first value [1]: https://i.stack.imgur.com/BreYy.png

CodePudding user response:

Have you tried adding as String?

questions[_questionIndex]['questionText'] as String

CodePudding user response:

Your class Question requires string parameter in constructor.

questions[_questionIndex]['questionText'] as String would fix the issue.

Cleaner solution would be to define questions variable as List<Map<String, dynamic>> questions or even create a separate model for question to avoid double indexes.

class QuestionModel {
  String questionText;
  List<String> answers;

  QuestionModel({required this.questionText, required this.answers});
}

and the define questions as

List<QuestionModel> questions = [
  QuestionModel(
    questionText: "What is your favourite color?",
    answers: ["Red", "Green", "Blue", "Yellow"],
  ),
  ...
];

to access the question from the list:

questions[_questionIndex].questionText

CodePudding user response:

questions[_questionIndex]['questionText'].toString();
  • Related