首页 > 解决方案 > 试图在列表中获取数据,我收到一个错误:未定义命名参数

问题描述

我学会了颤振并使用问答游戏教程。然后我试图在一个列表中获取问题列表,我的调试控制台返回我未定义的名称参数。我尝试了不同的解决方案,但我仍然有同样的问题

我的代码

import 'package:flutter/animation.dart';
import 'package:get/get.dart';
import 'package:quizz_app/models/question.dart';

//we use get pckage for our state management
class QuestionController extends GetxController
with SingleGetTickerProviderMixin {
late AnimationController _animationController;
late Animation _animation;
Animation get animation => _animation;
// ignore: prefer_final_fields
Iterable _question = sampleData
  .map((question) => Question(
      id: question['id'],
      answer: question['answer_id'],
      question: question['question'],
      options: question['option']))
  .toList();

 @override
 void onInit() {
 _animationController =
    AnimationController(duration: const Duration(seconds: 60), vsync: this);
_animation = Tween(begin: 1.0, end: 0.0).animate(_animationController)
  ..addListener(() {
    //update like setstate
    update();
  });
_animationController.forward();
super.onInit();
 }
 }

这是我的问题清单

class Question {
final int id, answer;
final String question;
final List<String> options;

Question(this.id, this.answer, this.question, this.options);
}

const List sampleData = [
{
  "id": 1,
  "question":
    "Flutter is an open-source UI software development kit created by ______",
  "options": ['Apple', 'Google', 'Facebook', 'Microsoft'],
  "answer_index": 1,
  },
  {
   "id": 2,
   "question": "When google release Flutter.",
   "options": ['Jun 2017', 'Jun 2017', 'May 2017', 'May 2018'],
   "answer_index": 2,
 },
 {
  "id": 3,
  "question": "A memory location that holds a single letter or number.",
   "options": ['Double', 'Int', 'Char', 'Word'],
   "answer_index": 2,
   },
  {
"id": 4,
"question": "What command do you use to output data to the screen?",
"options": ['Cin', 'Count>>', 'Cout', 'Output>>'],
 "answer_index": 2,
 },
];

标签: flutter

解决方案


question: question[question]

Iterable _question = sampleData
  .map((question) => Question(
      id: question['id'],
      answer: question['answer_id'],
      question: question[question], // This
      options: question['option']))
  .toList();

应该

question: question['question']

推荐阅读