【问题标题】:Why the app produced by flutter build web sometimes doesn't work?为什么flutter build web制作的app有时候不能用?
【发布时间】:2019-12-24 22:38:24
【问题描述】:

我有 2 个问题仅在执行 flutter build web 时出现。

  1. 有时flutter build web 无法(错误地)抱怨类型不兼容(见下文)。
  2. 有时构建过程完成,但 Web 应用程序无法运行:不显示任何内容,控制台中也没有消息。

我提到的错误是这样的:

% flutter build web
Target dart2js failed: Exception: lib/main.dart:24:31:                  
Error: A value of type 'ApiUsersRepository' can't be assigned to a variable of type 'UsersRepository'.
 - 'ApiUsersRepository' is from 'package:my_app/api_users_repo.dart' ('lib/api_users_repo.dart').
 - 'UsersRepository' is from 'lib/users_repo.dart'.
  final UsersRepository usersRepository = ApiUsersRepository();
                              ^
Error: Compilation failed.

该应用在开发时可以在 iOS 和 Web 中运行。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    解决方案

    我更改了所有文件的导入,例如:

    import 'package:my_app/users_repo.dart';
    

    收件人:

    import 'users_repo.dart';
    

    更多详情

    调查有关类型的错误,我发现了这个issue,其中重要的部分是这条评论:将每个导入更改为相对格式后,它解决了我的问题

    所以我这样做了,它解决了 2 个问题,编译错误和运行时错误。

    【讨论】:

    • 发生这种情况的原因是什么?
    • 我不确定。我认为 dart2js 编译器认为这些包是不同的。那里一定是一个错误。
    【解决方案2】:

    对我来说,我必须删除一个损坏的包。 c:\src\flutter.pub-cache\hosted\pub.dartlang.org\localstorage-4.0.0+1 显然,一个文件已被我反向损坏。我删除了这个包,做了一个颤振的 pub,然后重新编译,它工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-22
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 2011-06-12
      • 2023-04-06
      • 2012-10-09
      相关资源
      最近更新 更多