Home > Mobile >  The argument type 'List<dynamic>' can't be assigned to the parameter type '
The argument type 'List<dynamic>' can't be assigned to the parameter type '

Time:07-13

here is basically the entire code which is related to the error I suppose!! I am new to programming as well as Flutter, I tried to splitting the code into widgets so my code be cleaner. that's where I got stuck by this message: The argument type 'List' can't be assigned to the parameter type 'List<Map<String, Object>>'. I hope it's clear now to be checked. thanks!

my Main.dart code

Quiz.dart code

import 'package:flutter/material.dart';
import 'package:quiz2/ui/question.dart';
import 'package:quiz2/ui/quiz.dart';
import 'package:quiz2/ui/result.dart';
import './ui/answer.dart';

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

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _index = 0;
  final List _questions = const [
    {
      'questionText': 'who\'s your favorite football player? ',
      'Answers': ['Cristiano', ' Messi', 'Mbappe', 'Benzema']
    },
    {
      'questionText': 'who\'s your favorite Ping Pong player',
      'Answers': [
        'Noshad Alamian',
        'Nima Alamian',
        'Aria Amiri',
        'Matin Lotfollah Nassabi'
      ]
    },
    {
      'questionText': 'what\'s your favorite color',
      'Answers': ['Grey', 'Black', 'Green', 'Red']
    },
    {
      'questionText': 'what\'s your favorite football club',
      'Answers': ['Real Madrid', 'Barcelona', 'Manchester United', 'Liverpool']
    }
  ];
  void _answerQuestion() {
    if (_index < _questions.length) {
      debugPrint('we have more questions');
    } else {
      debugPrint('no more questions');
    }
    setState(() {
      _index = _index   1;
    });
    debugPrint('answered !!');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Color.fromARGB(255, 115, 147, 163),
          title: Text('Quiz App'),
          centerTitle: true,
        ),
        backgroundColor: Colors.blueGrey.shade600,
        body: Container(
            child: _index < _questions.length
                ? Quiz(
                    index: _index,
                    answerQuestion: _answerQuestion,
                    questions: _questions)
                : Result()),
      ),
    );
  }
}

CodePudding user response:

Problem might be with Flutter not properly defining type of your initialized list because you are creating type List (without any further specified generic type).

Change from

 final List _questions = const [ ...

To

final _questions = const <Map<String,Object>>[ ...

Or

 final List<Map<String,Object>> _questions = const [ ...

Should do the trick

  • Related