Home > Blockchain >  The argument type 'Object?' can't be assigned to the parameter type 'String'
The argument type 'Object?' can't be assigned to the parameter type 'String'

Time:08-05

import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';

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

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

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

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

  @override
  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': "What\'s your favourite color?",
        'answer': ['Black', 'Red', 'Green', 'White'],
      },
      {
        'questionText': 'What\'s your fav animal?',
        'answer': ['Wolf', 'Cat', 'Dog', 'Tiger'],
      },
      {
        'questionText': 'Who is your fav instructor?',
        'answer': ['Max', 'Max', 'Max', 'Max'],
      }
    ];
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('My first App'),
      ),
      body: Column(
        children: [
          Question(
            questions[_questionIndex]['questionText'],
          ),
          Answer(_answerQuestion),
          Answer(_answerQuestion),
          Answer(_answerQuestion),
        ],
      ),
    ));
  }
}

hello guys i am having issues in this code @ questions [_questionIndex]['questionText'], please help me out from this with correct solution with proper explanation. and if u can give me the cause of it then it be better for me to understand

CodePudding user response:

Your Question expect a string. You can do it like

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

Also better to accept nullable data.

Question(
  questions[_questionIndex]['questionText'] as String? ?? "Null",
),
  • Related