簡體   English   中英

參數類型&#39;列表<dynamic> &#39; 不能分配給參數類型 &#39;List <Map<String, Object> &gt;&#39;

[英]The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Map<String, Object>>'

這里基本上是與我想的錯誤相關的整個代碼! 我是編程和 Flutter 的新手,我嘗試將代碼拆分為小部件,以便我的代碼更干凈。 這就是我被這條消息卡住的地方:無法將參數類型“List”分配給參數類型“List<Map<String, Object>>”。 我希望現在可以檢查清楚。 謝謝!

我的 Main.dart 代碼

Quiz.dart 代碼

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()),
      ),
    );
  }
}

問題可能在於 Flutter 沒有正確定義初始化列表的類型,因為您正在創建類型 List(沒有任何進一步指定的泛型類型)。

從改變

 final List _questions = const [ ...

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

或者

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

應該做的伎倆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM