小编典典

Flutter:无法将类型为lib1 :: Object的值分配给类型为lib2 :: Object的变量

flutter

尝试编译此代码时:

import 'package:flutter/material.dart';
import 'package:startup_namer/misc/Constants.dart';
import 'package:startup_namer/ui/question_text.dart';
import '../utils/question.dart';
import '../utils/quiz.dart';
import '../ui/answer_button.dart';
import '../ui/correct_wrong_overlay.dart';

class QuizPage extends StatefulWidget {
    @override
    State createState() => new QuizPageState();
}

//States are mutable
class QuizPageState extends State<QuizPage> {

    static List<Question> quizQuestions = [
        new Question("5 * 5 = 25", true),
        new Question("4 + 2 = 6", true),
        new Question("4 + 2 = 7", false),
        new Question("Computers don't use energy", false),
        new Question("B is after A in the alphabet", false),
    ];

    Question currentQuestion;
    Quiz quiz = new Quiz(quizQuestions);

    String questionText;
    int questionNumber;
    bool isCorrect, overlayShouldBeVisible = false;


    @override
    void initState() {
        super.initState();
        this.currentQuestion = this.quiz.nextQuestion;
        this.questionText = this.currentQuestion.aQuestion;
        this.questionNumber = this.quiz.questionNumber;
    }

    @override
    Widget build(BuildContext context) {
        return new Stack( 
            fit: StackFit.expand,
            children: <Widget>[
                //Container for tons of stuff (Main page essentially)
                new Column(
                    children: <Widget>[
                        new AnswerButton(true,  () => testMe(true)),
                        new QuestionText(this.questionText, this.questionNumber),
                        new AnswerButton(false, () => testMe(false)),
                    ], //<Widget>[]
                ), //Column
                (this.overlayShouldBeVisible) ? new CorrectWrongOverlay(true) : new Container()
            ] //<Widget>[]
        ); //Stack
    }
}

void testMe(bool isTrue){
    if(isTrue){
        print("it is true, huzzah!");
    } else {
        print("it is false, :(");
    }
}

我收到此错误:

Error: A value of type 'dart.core::List<#lib1::Question>' can't be assigned to a variable of type 'dart.core::List<#lib2::Question>'.

我正在按照教程系列进行学习,并已针对其存储库检查了代码,但无法查看导致此问题的原因。

如果没有其他与之冲突的模棱两可的 Question 类,为什么会看到此强制转换错误?


阅读 277

收藏
2020-08-13

共1个答案

小编典典

事实证明,这个问题与进口的申报方式如何有关。

我在其他地方没有看到此问题,因此我将在此处回答问题。从这些更新我的进口:

import '../utils/question.dart';
import '../utils/quiz.dart';
import '../ui/answer_button.dart';
import '../ui/correct_wrong_overlay.dart';

对这些

import 'package:startup_namer/utils/question.dart';
import 'package:startup_namer/utils/quiz.dart';
import 'package:startup_namer/ui/answer_button.dart';
import 'package:startup_namer/ui/correct_wrong_overlay.dart';

我在使用完整的软件包声明而不是速记版本的地方似乎可以解决此问题。

2020-08-13