颤振构建找不到'dart:html'

Posted

技术标签:

【中文标题】颤振构建找不到\'dart:html\'【英文标题】:Flutter build cannot find 'dart:html'颤振构建找不到'dart:html' 【发布时间】:2020-04-28 16:01:15 【问题描述】:

我试过运行flutter clean。我尝试在betastable 频道之间进行切换。 编译器似乎正在尝试为浏览器支持添加一些东西,但我正在编译一个 APK。 Flutter Web 支持已禁用。我之前编译这个没有问题。看起来问题的根源可能是我正在使用的sentry 包。

如何让应用重新编译?

这是build apk的输出

$ flutter build apk
You are building a fat APK that includes binaries for android-arm, android-arm64, android-x64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size.
    To generate an app bundle, run:
        flutter build appbundle --target-platform android-arm,android-arm64,android-x64
        Learn more on: https://developer.android.com/guide/app-bundle
    To split the APKs per ABI, run:
        flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi
        Learn more on:  https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split

Compiler message:                                                       
../../flutter/.pub-cache/hosted/pub.dartlang.org/sentry-3.0.1/lib/src/browser.dart:7:8: Error: Not found: 'dart:html'
import 'dart:html' hide Event, Client;                                  
       ^                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart:6:8: Error: Not found: 'dart:html'
import 'dart:html';                                                     
       ^                                                                
../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart:34:18: Error: 'HttpRequest' isn't a type.
  final _xhrs = <HttpRequest>;                                        
                 ^^^^^^^^^^^                                            
../../flutter/.pub-cache/hosted/pub.dartlang.org/sentry-3.0.1/lib/src/browser.dart:51:19: Error: Getter not found: 'window'.
    origin ??= '$window.location.origin/';                            
                  ^^^^^^                                                
../../flutter/packages/flutter/lib/src/painting/_network_image_web.dart:88:12: Error: Method not found: 'webOnlyInstantiateImageCodecFromUrl'.
    return ui.webOnlyInstantiateImageCodecFromUrl(resolved, // ignore: undefined_function
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                          
../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart:58:34: Error: 'Blob' isn't a type.
      var blob = xhr.response as Blob ?? Blob([]);                      
                                 ^^^^                                   
../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart:46:15: Error: The method 'HttpRequest' isn't defined for the class 'BrowserClient'.
 - 'BrowserClient' is from 'package:http/src/browser_client.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart').
Try correcting the name to the name of an existing method, or defining a method named 'HttpRequest'.
    var xhr = HttpRequest();                                            
              ^^^^^^^^^^^                                               
../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart:58:42: Error: The method 'Blob' isn't defined for the class 'BrowserClient'.
 - 'BrowserClient' is from 'package:http/src/browser_client.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart').
Try correcting the name to the name of an existing method, or defining a method named 'Blob'.
      var blob = xhr.response as Blob ?? Blob([]);                      
                                         ^^^^                           
../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart:59:20: Error: The method 'FileReader' isn't defined for the class 'BrowserClient'.
 - 'BrowserClient' is from 'package:http/src/browser_client.dart' ('../../flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.1/lib/src/browser_client.dart').
Try correcting the name to the name of an existing method, or defining a method named 'FileReader'.
      var reader = FileReader();                                        
                   ^^^^^^^^^^                                           
Unhandled exception:                                                    
FileSystemException(uri=org-dartlang-untranslatable-uri:dart%3Ahtml; message=StandardFileSystem only supports file:* and data:* URIs)
#0      StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:33:7)
#1      asFileUri (package:vm/kernel_front_end.dart:567:37)             
#2      writeDepfile (package:vm/kernel_front_end.dart:760:21)          
<asynchronous suspension>                                               
#3      FrontendCompiler.compile (package:frontend_server/frontend_server.dart:536:15)
<asynchronous suspension>                                               
#4      _FlutterFrontendCompiler.compile (package:flutter_frontend_server/server.dart:40:22)
#5      starter (package:flutter_frontend_server/server.dart:178:27)    
#6      main (file:///b/s/w/ir/cache/builder/src/flutter/flutter_frontend_server/bin/starter.dart:8:30)
#7      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#8      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.                                                           

FAILURE: Build failed with an exception.                                

* Where:                                                                
Script '/home/werner/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 882

* What went wrong:                                                      
Execution failed for task ':app:compileFlutterBuildRelease'.            
> Process 'command '/home/werner/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org                              

BUILD FAILED in 28s                                                     
Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                      29.4s
Gradle task assembleRelease failed with exit code 1

【问题讨论】:

【参考方案1】:

我最终发现了我的错误。 我的错误是明确导入

import 'package:sentry/browser_client.dart';

而不是

import 'package:sentry/sentry.dart';

【讨论】:

好一个。 Dio 也一样:使用网络和移动设备时必须有条件地导入 'package:dio/adapter_browser.dart'【参考方案2】:

如果您需要在这种情况或其他情况下同时使用,可以这样导入:

import 'package:sentry/sentry.dart' if (dart.library.html) 'package:sentry/browser_client.dart' as sentry;

【讨论】:

以上是关于颤振构建找不到'dart:html'的主要内容,如果未能解决你的问题,请参考以下文章

错误:在颤振中找不到类型“ui.TextHeightBehavior”

颤振医生找不到android studio

在 Flutter SDK 中找不到 Dart

Flutter / VSC 在你的 Flutter SDK 中找不到 Dart

在颤振中为Android构建时“错误:找不到符号”

构建到 IOS 的颤振时找不到 BoringSSL-GRPC.modulemap