Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: A value of type lib1::Object can't be assigned to a variable of type lib2::Object

Tags:

flutter

dart

While trying to compile this code:

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

I am receiving this error:

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.

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

like image 679
PGMacDesign Avatar asked Jun 18 '18 16:06

PGMacDesign


People also ask

How do you know the type of object in flutter?

To check the type of a variable in Flutter and Dart, you can use the runtimeType property.

How do you assign a null to a variable in flutter?

We cannot assign a null value to a variable because it'll throw a compilation error: final String country = null; // Cannot be null.


1 Answers

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

to these

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.

like image 148
PGMacDesign Avatar answered Oct 24 '22 01:10

PGMacDesign