代码之家  ›  专栏  ›  技术社区  ›  zipzit

省道/颤振列表中的左侧和右侧编译器错误

  •  3
  • zipzit  · 技术社区  · 6 年前

    我很难让这个程序运行。我在运行代码时总是遇到编译器问题。具体见下文。有人知道这是怎么回事吗?

    从文件中提取我的代码 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();
    }
    

    文件中的profile类 profiles.dart (没有导入语句):

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

    文件中的我的配置文件列表 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
        ),
    ]
    

    所有这些都会被叫来 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
        );
    

    启动时收到编译器消息:

    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,`
    

    (List <Profile>) 但这似乎是个失败,或者我只是做错了。

    注意:当我注释掉错误行并调试步骤到失败位置时,我可以看到 keyOne demoProfiles 完全符合预期。注意,这些代码片段位于不同的文件中,通过 import 命令。

    当我查看其余代码时,我可以看到 import 'profiles.dart' 多次调用。

    我不明白错误信息。我也学过 this posting with a similar error,

    1 回复  |  直到 6 年前
        1
  •  4
  •   Peter Haddad    6 年前

    您需要更改 main.dart :

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

    自从 只能有 packages: 称为绝对进口的进口。

    因此,将导入更改为:

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

    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