'Error: The argument type 'List<String>' can't be assigned to the parameter type 'String'

I am a beginner in dart and I am trying to make a quiz game application where I use some widgets to get questions and answer options on the screen. The error that I am facing is "The argument type 'List' can't be assigned to the parameter type 'String'" Here are some snippets of my code:-

  1. This is a list of Maps<String, Object> , I am storing questions and their answer options in this variable.
var questions = [
      {
        'questionText': 'What is your favourite season?',
        'answers': [
          'Summer',
          'Monsoon',
          'Winter',
        ]
      },
      {
        'questionText': 'What is your favourite color?',
        'answers': [
          'Black',
          'Green',
          'Blue',
        ]
      },
      {
        'questionText': 'What is your favourite food?',
        'answers': [
          'Chicken',
          'Kebab',
          'Biryani',
        ]
      },
    ];
  1. This is where I call the Question and Answer Widget.
body: Column(
          children: [
            Question(questions[_questionsIndex]["questionText"]),
            ...{questions[_questionsIndex]['answers'] as List<String>}
                .map((answer) {
              return Answer(_answerQuestion, answer); //**ERROR**
            }).toList(),
          ],
        ),

The error appears on the statement : return Answer(_answerQuestion, answer);

  1. This is how my Answer Widget Class looks like:
class Answer extends StatelessWidget {
  final Function selectAnswer;
  final String answerText;

  Answer(this.selectAnswer, this.answerText);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          primary: Colors.blue,
          elevation: 0.0,
        ),
        child: Text(answerText,
            style: TextStyle(
              color: Colors.black,
            )),
        onPressed: () {
          selectAnswer();
        },
      ),
    );
  }
}

What is the meaning of this error and why does it appear? How can I fix this error?

Thank you!



Solution 1:[1]

I think the problem is in your second code block. For a list of string, you should use just List type Instead of List<String> type.

You code would be like:

body: Column(
          children: [
            Question(questions[_questionsIndex]["questionText"]),
            ...{questions[_questionsIndex]['answers'] as List}
                .map((answer) {
              return Answer(_answerQuestion, answer); //**ERROR**
            }).toList(),
          ],
        ),

I hope this can solve your problem.

Solution 2:[2]

You could try especifying the type of your main list with List<Map<String,dynamic>>

List<Map<String,dynamic>> questions = [
  {
    'questionText': 'What is your favourite season?',
    'answers': [
      'Summer',
      'Monsoon',
      'Winter',
    ]
  },
  {
    'questionText': 'What is your favourite color?',
    'answers': [
      'Black',
      'Green',
      'Blue',
    ]
  },
  {
    'questionText': 'What is your favourite food?',
    'answers': [
      'Chicken',
      'Kebab',
      'Biryani',
    ]
  },
];

I tried to assing the element to a List<String> and every works

var a = questions[1]['answers'];
List<String> b = a;
print(b);

Solution 3:[3]

if you mean: The argument type 'List<String?>?' can't be assigned to the parameter type 'List<String?>'.

I solve this issue with a single line, you can get List<String?> from List<String?>?

List<String?>? mylist = ['data1','data2'];
List<String?> mylist2 = List<String>.from(mylist);

Solution 4:[4]

Algorithms will remain same as any other classification but you can do the try following:

  1. Do dimensionality reduction using PCA to reduce dimensions

  2. Use forward or backward selection algorithms

  3. Remove highly correlated variable

  4. Use L1 regularisation with high alpha value as it does features selection intinsically

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zichzheng
Solution 2 José Zenteno
Solution 3 Ramy Wahid
Solution 4 Ashwiniku918