Flutter 为 Web 构建 - "Failed to compile application"

Flutter Build For Web - "Failed to compile application"

我为我的项目启用了网络支持,在 运行 flutter run -d chrome 之后我收到以下错误:

Launching lib/main.dart on Chrome in debug mode...
Syncing files to device Chrome...                                                                                                                                                                    
Compiler message:                                  
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapiauth2.dart:26:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  GoogleAuth.fakeConstructor$();                                                                                   
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapiauth2.dart:216:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  SigninOptionsBuilder.fakeConstructor$();                                                                         
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:351:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  HttpRequestPromise.fakeConstructor$();                                                                           
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:378:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  HttpRequest.fakeConstructor$() : super.fakeConstructor$();                                                       
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:402:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  HttpBatch.fakeConstructor$();                                                                                    
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:440:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  RpcRequest.fakeConstructor$();                                                                                   
  ^                                                                                                                
Syncing files to device Chrome...                               19,442ms (!)                                       
Failed to compile application.
Tals-Macbook:matkonit talbarda$ flutter run -d chrome
Launching lib/main.dart on Chrome in debug mode...
Syncing files to device Chrome...                                                                                                                                                                    
Compiler message:                                  
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapiauth2.dart:26:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  GoogleAuth.fakeConstructor$();                                                                                   
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapiauth2.dart:216:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  SigninOptionsBuilder.fakeConstructor$();                                                                         
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:351:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  HttpRequestPromise.fakeConstructor$();                                                                           
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:378:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  HttpRequest.fakeConstructor$() : super.fakeConstructor$();                                                       
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:402:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  HttpBatch.fakeConstructor$();                                                                                    
  ^                                                                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.8.3+1/lib/src/generated/gapi.dart:440:3: Error: JS interop classes do not support non-external constructors.
Try annotating with `external`.                                                                                    
  RpcRequest.fakeConstructor$();                                                                                   
  ^                                                                                                                
Syncing files to device Chrome...                               17,899ms (!)                                       
Failed to compile application.

我该如何解决这个问题?

改用 Beta 频道。

flutter channel beta

我注意到此问题已在 google_sign_in_web 0.9.0 中修复。因此,请更改 pubspec.yaml 文件中的版本。

google_sign_in_web: ^0.9.1

2020 年 4 月 20 日更新:如果您依赖 google_sign_in,请在 pubspec.yaml 文件中更改该版本。

google_sign_in: 4.4.3

之前: 但是,如果您像我一样并且对 google_sign_in 有依赖性,那么您还有更多的东西需要更改... 如果将 google_sign_in_web 更改为 0.9.0,并将 google_sign_in 的版本更改为 4.4.1,则检索包将失败:

Because ${your_project_name} depends on google_sign_in 4.4.1 which depends on google_sign_in_web ^0.8.2, google_sign_in_web ^0.8.2 is required.
So, because ${your_project_name} depends on google_sign_in_web ^0.9.0, version solving failed.
pub get failed (1; So, because  ${your_project_name} depends on google_sign_in_web ^0.9.0, version solving failed.)

在这种情况下,您将不得不等待 google_sign_in 升级(我创建了 pull request https://github.com/flutter/plugins/pull/2647),或者暂时像我一样:

1) 从 https://github.com/flutter/plugins

克隆 flutter 插件
mkdir /src
cd /src
git clone https://github.com/flutter/plugins.git

2) 在/src/plugins/plugins/packages/google_sign_in/google_sign_in/pubspec.yaml

中更改文件pubspec.yaml
  google_sign_in_web: ^0.9.0

3) 更改项目中的依赖项以指向 google_sign_in 的更改版本,即

google_sign_in:
  path: ../../flutter/plugins/packages/google_sign_in/google_sign_in
google_sign_in_web: ^0.9.0

由于某些原因,较新版本的 flutter 停止支持这种格式的依赖项: folding_cell: "^0.1.2" 相反,请检查您的 pubspec.yaml 并将具有该格式的任何依赖项更改为: folding_cell: ^1.0.0 我以 folding_cell 为例。 这解决了我的问题,希望它能帮助别人。

使用 dart pub outdated 来识别过时的包依赖项并获取有关如何更新它们的建议。依赖项管理的最佳实践包括使用最新的稳定包版本,因此您可以获得最新的错误修复和改进。

打开终端和应用程序文件夹的路径 和 运行 flutter pub outdated 列出所有过时的包。

cmd:flutter pub outdated

并将所有过时的软件包升级到最新或可解析的版本。 和 运行 来自终端 flutter pub 升级。 将过时的软件包更新为最新的。 然后 运行 您的应用。

more info:https://dart.dev/tools/pub/cmd/pub-outdated