Flutter:无法将类型为lib1 :: Object的值分配给类型为lib2 :: Object的变量 [英] Flutter: A value of type lib1::Object can't be assigned to a variable of type lib2::Object

查看:229
本文介绍了Flutter:无法将类型为lib1 :: Object的值分配给类型为lib2 :: Object的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试编译此代码时:

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>'.

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

I am following this tutorial series and have checked the code against their Repository but am unable to see what is causing this problem.

如果出现以下情况,为什么我会看到此转换错误没有其他模棱两可的 Question 类与此冲突吗?

Why would I be seeing this casting error if there are no other ambiguous Question classes that conflict with it?

推荐答案

事实证明,该问题必须解决

It turns out the issue had to do with how the imports were being declared up top.

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

I have not seen this mentioned elsewhere so I will answer the question here. Updating my imports from these:

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';

在我使用完整软件包声明而不是简写形式的地方,似乎可以解决此问题。

Where I am using the full package declaration as opposed to the shorthand version seems to resolve the issue.

这篇关于Flutter:无法将类型为lib1 :: Object的值分配给类型为lib2 :: Object的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆