'The argument type 'Object?' can't be assigned to the parameter type 'String' latest version

Hers is the part from code.dart:

final List<Map< String,Object>> question = [
    {
      'questionText': 'what\'s your favorite\'s color?',
      'answers': [
        'Black',
        'Green',
        'Blue',
        'Yellow',
      ]
    },
    {
      'questionText': 'Select a true fact about Paulo Maldini!',
      'answers': [
        'He is dead',
        'He is alive',
        'He killed',
        'He is single',
      ]
    },
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: MyText("Quiz Bahlol", fontstyle2),
          ),
          body: Container(
            width: double.infinity,
            child: Column(
              children: <Widget>[
                Question(question[_questionIndex]['questionText']),
            // in this line error
   question[_questionIndex]['answers'].map((answer) {
                  return Answer(answerQuestion, answer);
                }).toList(),
              ],
            ),
          )),
    );

And give me this error when run the app:

Expected a value of type 'widget' , but got one of type 'List'

But in older versions is working ok. The argument type 'Object?' can't be assigned to the parameter type 'String'.



Solution 1:[1]

You should try this:

Change this:

final List<Map< String,Object>> question = [
{
  'questionText': 'what\'s your favorite\'s color?',
  'answers': [
    'Black',
    'Green',
    'Blue',
    'Yellow',
  ]
},
{
  'questionText': 'Select a true fact about Paulo Maldini!',
  'answers': [
    'He is dead',
    'He is alive',
    'He killed',
    'He is single',
  ]
},

];

to:

var question = [
    {
      'questionText': 'what\'s your favorite\'s color?',
      'answers': [
        'Black',
        'Green',
        'Blue',
        'Yellow',
      ]
    },
    {
      'questionText': 'Select a true fact about Paulo Maldini!',
      'answers': [
        'He is dead',
        'He is alive',
        'He killed',
        'He is single',
      ]
    },
  ];

and also change:

Question(question[_questionIndex]['questionText']),

to:

Question(question[_questionIndex]['questionText'] as String),

Solution 2:[2]

The problem is in your Column widget. You should either use

Container(
  width: double.infinity,
  child: Column(
    children: <Widget>[
      Question(question[_questionIndex]['questionText']),
      ...question[_questionIndex]['answers'].map((answer) {
        return Answer(answerQuestion, answer);
      }),
    ],
  ),
)
Container(
  width: double.infinity,
  child: Column(
    children: <Widget>[
      Question(question[_questionIndex]['questionText']),
      for (var answer in question[_questionIndex]['answers'])
        Answer(answerQuestion, answer)
    ],
  ),
)

Solution 3:[3]

Your column's children is being created with a List within a List, where it should literally be only one List.

Easiest solution with little code editing:

children: <Widget>[
   Question(question[_questionIndex]['questionText']),
 ] + List.generate(question[_questionIndex]['answers'].length, (index) => 
    Answer(answerQuestion, answer))
       
 

Solution 4:[4]

if you are using a an updated sdk version of dart, then add '' as String'' to your questions ''Question(question[_questionIndex]['questionText']),

change it to ''Question(question[_questionIndex]['questionText']'as String, ''),

but more importantly change your Text in the button to ''answerText

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 Dharman
Solution 2
Solution 3 Spots Solutions
Solution 4 Joey