Home > OS >  ib/main.dart:49:24: Error: The operator '[]' isn't defined for the class 'Set<
ib/main.dart:49:24: Error: The operator '[]' isn't defined for the class 'Set<

Time:12-06

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

void main() {
  runApp(MyFirstApp());
}

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

class _MyFirstAppState extends State<MyFirstApp> {
  var _questionIndex = 0;
  void _ansButtonPress() {
    setState(() {
      _questionIndex = _questionIndex   1;
    });
  }

  var questions = [
    {
      'questionText': 'What is  your favorate Colour',
      'answers': ['red', 'balck ', 'yellow', 'blue']
    },
    {
      'questionText': 'What is  your favorate Animal',
      "answers": ['Lion', 'Chettah ', 'Leopard', 'Cat']
    },
    {
      'questionText': 'What is  your favorate Movie',
      'answers': ['hai', 'hello ', 'hoe', 'are you']
    }
  ];

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My first app '),
        ),
        body: Column(
          children: [
            Question(
              questions[_questionIndex]['questionText'],
            ),
            Answer(_ansButtonPress),
            Answer(_ansButtonPress),
            Answer(_ansButtonPress),
          ],
        ),
      ),
    );
  }
}

error in line 49 Screenshot

terminal showing like this

lib/main.dart:49:24: Error: The operator '[]' isn't defined for the class 'Set<Map<String, Object>>'. - 'Set' is from 'dart:core'. - 'Map' is from 'dart:core'. - 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator. questions[_questionIndex]['questionText'], ^ Failed to package /Users/j3rry01/Documents/Flutter Lab/flutter basics /new_flutter. Command PhaseScriptExecution failed with a nonzero exit code note: Using new build system note: Planning note: Build preparation complete note: Building targets in parallel warning: Capabilities for Signing & Capabilities may not function correctly because its entitlements use a placeholder team ID. To resolve this, select a development team in the Runner editor. (in target 'Runner' from project 'Runner')

i am a beginner , not so good with the dart concepts , i don't see any error on my POV

CodePudding user response:

From the error message it appears that the questions variable is been taken as a Set and not as a List. Try setting its type explicitly like this:

List<Map<String, dynamic>> questions = [
    {
      'questionText': 'What is  your favorate Colour',
      'answers': ['red', 'balck ', 'yellow', 'blue']
    },
    {
      'questionText': 'What is  your favorate Animal',
      "answers": ['Lion', 'Chettah ', 'Leopard', 'Cat']
    },
    {
      'questionText': 'What is  your favorate Movie',
      'answers': ['hai', 'hello ', 'hoe', 'are you']
    }
];

Additionally if you're not going to change the value of the questions variable it's better to declare it as final

  • Related