【问题标题】:type '_CompactLinkedHashSet<Question>' is not a subtype of type 'List<Question>' in type cast类型“_CompactLinkedHashSet<Question>”不是类型转换中“List<Question>”类型的子类型
【发布时间】:2021-11-07 05:38:05
【问题描述】:

我遇到了这个错误,它运行完美,但没有输出并显示 跟随错误 类型“_CompactLinkedHashSet”不是类型转换中“List”类型的子类型 颤动

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:quizler/Question.dart';
import 'Question.dart';

void main() => runApp(quizler());

class quizler extends StatelessWidget {
  const quizler({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.grey.shade900,
        body: SafeArea(
          child: QuizzPage(),
        ),
      ),
    );
  }
}

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

  @override
  _QuizzPageState createState() => _QuizzPageState();
}

class _QuizzPageState extends State<QuizzPage> {
  List<Widget> scoreKeeper = [];
 List<Question> questionBank={
   Question(q: 'Moon Color is gray',a:false),
   Question(q: 'Independence Day of pakistan was 1947',a:true),
   Question(q: 'Tesla was the owner of Starlink company',a:false),
 } as List<Question> ;
  int questionNumber = 0;
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Expanded(
          flex: 5,
          child: Padding(
            padding: EdgeInsets.all(15.0),
            child: Center(
              child: Text(
                questionBank[questionNumber].questionText,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 25.0,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(20.0),
            child: FlatButton(
              textColor: Colors.white,
              color: Colors.green,
              onPressed: () {
                bool correctAnswer = questionBank[questionNumber].questionAnswer;
                if (correctAnswer == true) {
                  setState(() {
                    questionNumber++;
                    scoreKeeper.add(
                      Icon(Icons.check, color: Colors.green),
                    );
                  });
                } else {
                  setState(() {
                    questionNumber++;
                    scoreKeeper.add(
                      Icon(
                        Icons.check,
                        color: Colors.red,
                      ),
                    );
                  });
                }
              },
              child: Text(
                'True',
              ),
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(20.0),
            child: FlatButton(
              textColor: Colors.white,
              color: Colors.red,
              onPressed: () {
                bool correctAnswer = questionBank[questionNumber].questionAnswer;
                if (correctAnswer == false) {
                  setState(
                    () {
                      questionNumber++;
                      scoreKeeper.add(
                        Icon(Icons.check, color: Colors.green),
                      );
                    },);
                }
                else{
                  setState(
                        () {
                      questionNumber++;
                      scoreKeeper.add(
                        Icon(Icons.check, color: Colors.red),
                      );
                    },);
                }
              },
              child: Text(
                'False',
              ),
            ),
          ),
        ),
        Row(
          children: scoreKeeper,
        ),
      ],
    );
  }
}

**Here is the Question.dart code**
    > Question.dart
    
    class Question{   late String questionText;   late bool
    questionAnswer;  Question({required String q, required bool a})   { 
    questionText=q;    questionAnswer=a;   } }'

在构建 MediaQuery(MediaQueryData(size: Size(360.0, 736.0), devicePixelRatio: 3.0, textScaleFactor: 1.0, platformBrightness: Brightness.dark, padding: E​​dgeInsets.zero, viewPadding: E​​dgeInsets.zero, viewInsets: EdgeInsets.zero,alwaysUse24HourFormat:false,accessibleNavigation:false,highContrast:false,disableAnimations:false,invertColors:false,boldText:false,navigationMode:传统)): 类型“_CompactLinkedHashSet”不是类型转换中“List”类型的子类型

【问题讨论】:

    标签: flutter flutter-android


    【解决方案1】:

    改成这样:

    List<Question> questionBank=[
       Question(q: 'Moon Color is gray',a:false),
       Question(q: 'Independence Day of pakistan was 1947',a:true),
       Question(q: 'Tesla was the owner of Starlink company',a:false),
     ]
    

    使用方括号。或将您的定义更改为:

    Set<Question> questionBank = { ....etc };
    

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 2021-12-30
      • 2021-06-04
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2019-01-13
      • 2021-12-06
      相关资源
      最近更新 更多