Home > database >  Expected to find ']'. Tried other answers which were present but it is not working
Expected to find ']'. Tried other answers which were present but it is not working

Time:10-01

I am not understanding what is going wrong in this code, I am new to the language so I am not really getting this, If someone could tell me in simple words, I would be grateful.: Error is on line ").tolist()"

class _MyAppState extends State<MyApp> {
      
      var _questionindex = 0;
      void answerques() {
        setState(() {
          
          _questionindex = _questionindex   1;
        });
    
        print(_questionindex);
      }
    
      @override
      Widget build(BuildContext context) {
        var questions = [
          {
            'questionText': 'What is your favourite color',
            'answers': ['Black', 'Red', 'Green', 'White']
          },
          {
            'questionText': 'What is your favourite Animal',
            'answers': ['Tiger', 'Bull', 'Cat', 'Dog']
          },
        ]; 
        return MaterialApp(
          home: Scaffold(
           
            appBar: AppBar(
              
              title: Text(
                  'My First App'), 
            ),
            body: Column(children: [
              Question(questions[_questionindex]['questionText']?.toString() ?? '', ), 
              ...(questions[_questionindex]['answers'] as List<String>).map((answer) => Answer(answerques, answer)  )         
            ).tolist()
            ]), 
          ),
        ); 
      }
    }
     

CodePudding user response:

You should remove one parenthesis, so your return should look like this:

return MaterialApp(
  home: Scaffold(
    appBar: AppBar(
      title: Text('My First App'), 
    ),
    body: Column(children: [
      Question(questions[_questionindex]['questionText']?.toString() ?? ''), 
      ...(questions[_questionindex]['answers'] as List<String>).map((answer) => Answer(answerques, answer)).toList(),
    ]), 
  ),
); 

CodePudding user response:

There are simply two typo-mistake

  • having extra )
  • type mistake of toList.

Answer.

 ...(questions[_questionindex]['answers'] as List<String>)
              .map((answer) => Answer(answerques, answer))
              .toList()
  • Related