Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left Hand Side , Right Hand Side Compiler Errors in Dart / Flutter List

Tags:

flutter

dart

I'm having a terrible time getting this program to run. I keep getting compiler troubles when I run the code. See specifics below. Anybody understand what's going on here?

My code extract from file TapScreenToo.dart:

import 'profiles.dart';
import 'package:flutter/material.dart';
import 'cards.dart';

class PageTapScreen extends StatefulWidget {
  final List<Profile> profileList;
  Key key;

  PageTapScreen({
    this.key,
    this.profileList,
  }) : super(key: key);    // constructor

  @override
  _PageTapScreenState createState() => new _PageTapScreenState();
}

The profile class in file profiles.dart (no import statement there):

class Profile {
  final String ttaKey;
  final String imageUrl;
  final String displayName;

  Profile({
    this.ttaKey,
    this.imageUrl,
    this.displayName,
  });
}

my profile list from file demoProfiles.dart:

import 'profiles.dart';

final List<Profile> demoProfiles = [
  new Profile(
    ttaKey: "0",
    imageUrl: 'http://localhost:8080/photo_0.jpg',
    displayName: 'abc',
    ),
  new Profile(
    ttaKey: "1",
    imageUrl: 'http:// ... etc
    ),
]

And all of this gets called in main.dart

import 'package:flutter/material.dart';
import 'tapScreenToo.dart';
import 'demoProfiles.dart';

class _MyHomePageState extends State<MyHomePage> {
  final Key keyOne = PageStorageKey('pageOne');

...
@override
  void initState() {
    tapScreen = PageTapScreen(
      key: keyOne,
      profileList:  demoProfiles,        <--- error points here
    );

I'm getting a compiler message at the time of initiation:

compiler message: lib/main.dart:68:20: Error: A value of type 'dart.core::List<#lib1::Profile>' can't be assigned to a variable of type 'dart.core::List<#lib2::Profile>'.  
compiler message: Try changing the type of the left hand side, or casting the right hand side to 'dart.core::List<#lib2::Profile>'. 
compiler message:       profileList: demoProfiles,`

I've tried to cast (List <Profile>) but that seems to be a fail, or I'm just doing it wrong.

Note: when I remark out the error line, and debug step to the failure location, I can see that the values for both keyOne and demoProfiles are exactly as expected. Note, these code snippets are located in different files, linked via import commands.

As I look at the rest of the code, I can see where import 'profiles.dart' gets called multiple times.

I don't understand the error message. I've also studied this posting with a similar error, but I'm just not seeing what's going on with this code, nor how to fix it.

like image 764
zipzit Avatar asked Dec 18 '25 13:12

zipzit


1 Answers

You need to change the imports in the main.dart:

import 'tapScreenToo.dart';
import 'demoProfiles.dart';

Since the main.dart can only have packages: imports which are called absolute imports.

Therefore change the imports to:

import package:mypackage/path/tapScreenToo.dart
import package:mypackage/path/demoProfiles.dart

Check this:

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

https://github.com/dart-lang/sdk/issues/33076

https://www.dartlang.org/tools/pub/get-started#importing-libraries-from-packages

like image 116
Peter Haddad Avatar answered Dec 20 '25 10:12

Peter Haddad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!