Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve "The argument type 'Object?' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)"?

Tags:

flutter

dart

I am trying to access the values of questionText from questions. When I am trying to extract String values from a map, the following error is displayed on Flutter. (The code is written in Dart):

The argument type 'Object?' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)

Error:

enter image description here

This is my main.dart file:

import 'package:flutter/material.dart';

import './question.dart';

import './answer.dart';

// void main() {
//   runApp(MyApp());
// }

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _questionIndex = 0;

  void _answerQuestion() {
    setState(() {
      _questionIndex += 1;
    });
    print(_questionIndex);
  }

  @override
  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': 'What\'s your favorite color?',
        'answers': ['Black', 'Red', 'Green', 'White'],
      },
      {
        'questionText': 'What\'s your favorite animal?',
        'answers': ['Rabbit', 'Snake', 'Elephant', 'Lion'],
      },
      {
        'questionText': 'Who\'s your favorite instructor?',
        'answers': ['Max', 'Max', 'Max', 'Max'],
      },
    ];
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: Column(
          children: [

Error on this line:

    Question(
                  questions[_questionIndex]['questionText'],
                ),
                Answer(_answerQuestion),
                Answer(_answerQuestion),
                Answer(_answerQuestion),
              ],
            ),
          ),
        );
      }
    }

This is my question.dart file:

    import 'package:flutter/material.dart';
    
    class Question extends StatelessWidget {
      final String questionText;
    
      Question(this.questionText);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          width: double.infinity,
          margin: EdgeInsets.all(10),
          child: Text(
            questionText,
            style: TextStyle(fontSize: 28),
            textAlign: TextAlign.center,
          ),
        );
      }
    }
like image 828
Sohan Kundu Avatar asked Jan 21 '26 01:01

Sohan Kundu


2 Answers

You need to let dart know the type of questions[_questionIndex]['questionText']

Try this:

Change questions[_questionIndex]['questionText']

to questions[_questionIndex]['questionText'] as String

In the error line

Or You can rewrite as: questions[_questionIndex]['questionText'] ?? ''

like image 132
Asjad Siddiqui Avatar answered Jan 23 '26 21:01

Asjad Siddiqui


Replace question[_questionIndex]['questionText'] By question[_questionIndex]['questionText'] as String

like image 30
Azer Laribi Avatar answered Jan 23 '26 21:01

Azer Laribi