当前位置: 首页 > 知识库问答 >
问题:

Flutter, FileReader() 在编译到 Android 时崩溃

寇甫
2023-03-14

我正在开发一个多平台应用程序,它可以在Android、IOS和web上运行。我为每个平台调用了不同的服务,在其中一个服务中,我使用FileReader()从web版本上传文件,它可以工作,但如果我不评论FileReader(),它就不允许我编译Android版本,输出如下:

Launching lib/main.dart on Mi 9T in debug mode...
Running Gradle task 'assembleDebug'...
Unhandled exception:
Crash when compiling file:///Users/danielcardona/Flutter/ptf_online/ptf_online/lib/pages/datos_page/utils/filepicker_pc.dart,
at character offset 81:
RangeError (offset): Invalid value: Not in range 0..594, inclusive: 7359
#0      RangeError.checkValueInInterval (dart:core/errors.dart:283:7)
#1      Source.getLocation (package:kernel/ast.dart:7277:16)
#2      getLocation (package:front_end/src/fasta/messages.dart:16:52)
#3      ProcessedOptions.format (package:front_end/src/base/processed_options.dart:205:47)
#4      ProcessedOptions.report (package:front_end/src/base/processed_options.dart:228:29)
#5      CompilerContext.report (package:front_end/src/fasta/compiler_context.dart:69:13)
#6      Loader.addMessage (package:front_end/src/fasta/loader.dart:325:20)
#7      Loader.addProblem (package:front_end/src/fasta/loader.dart:287:12)
#8      LibraryBuilderImpl.addProblem (package:front_end/src/fasta/builder/library_builder.dart:284:19)
#9      SourceLibraryBuilder.addProblem (package:front_end/src/fasta/source/source_library_builder.dart:1250:47)
#10     BodyBuilder.addProblem (package:front_end/src/fasta/kernel/body_builder.dart:5611:20)
#11     BodyBuilder.addProblemErrorIfConst (package:front_end/src/fasta/kernel/body_builder.dart:5625:5)
#12     BodyBuilder.buildAbstractClassInstantiationError (package:front_end/src/fasta/kernel/body_builder.dart:5292:5)
#13     BodyBuilder.resolveRedirectingFactoryTargets (package:front_end/src/fasta/kernel/body_builder.dart:1125:13)
#14     BodyBuilder.finishFunction (package:front_end/src/fasta/kernel/body_builder.dart:983:5)
#15     DietListener.listenerFinishFunction (package:front_end/src/fasta/source/diet_listener.dart:908:14)
#16     DietListener.buildFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:941:7)
#17     DietListener.endTopLevelMethod (package:front_end/src/fasta/source/diet_listener.dart:352:5)
#18     Parser.parseTopLevelMethod (package:_fe_analyzer_shared/src/parser/parser_impl.dart:2518:14)
#19     Parser.parseTopLevelMemberImpl (package:_fe_analyzer_shared/src/parser/parser_impl.dart:2372:14)
#20     Parser.parseTopLevelDeclarationImpl (package:_fe_analyzer_shared/src/parser/parser_impl.dart:495:14)
#21     Parser.parseUnit (package:_fe_analyzer_shared/src/parser/parser_impl.dart:352:15)
#22     SourceLoader.buildBody (package:front_end/src/fasta/source/source_loader.dart:329:14)
<asynchronous suspension>
#23     Loader.buildBodies (package:front_end/src/fasta/loader.dart:243:15)
<asynchronous suspension>
#24     KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:307:20)
#25     withCrashReporting (package:front_end/src/fasta/crash.dart:122:24)
#26     KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:305:12)
#27     generateKernelInternal.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:149:38)
<asynchronous suspension>
#28     withCrashReporting (package:front_end/src/fasta/crash.dart:122:24)
#29     generateKernelInternal (package:front_end/src/kernel_generator_impl.dart:70:10)
#30     kernelForProgramInternal.<anonymous closure> (package:front_end/src/api_prototype/kernel_generator.dart:61:35)
#31     CompilerContext.runWithOptions.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:135:20)
<asynchronous suspension>
#32     CompilerContext.runInContext.<anonymous closure>.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:123:46)
#33     new Future.sync (dart:async/future.dart:224:31)
#34     CompilerContext.runInContext.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:123:19)
#35     _rootRun (dart:async/zone.dart:1126:13)
#36     _CustomZone.run (dart:async/zone.dart:1023:19)
#37     _runZoned (dart:async/zone.dart:1518:10)
#38     runZoned (dart:async/zone.dart:1465:12)
#39     CompilerContext.runInContext (package:front_end/src/fasta/compiler_context.dart:122:12)
#40     CompilerContext.runWithOptions (package:front_end/src/fasta/compiler_context.dart:133:10)
#41     kernelForProgramInternal (package:front_end/src/api_prototype/kernel_generator.dart:60:32)
#42     kernelForProgram (package:front_end/src/api_prototype/kernel_generator.dart:52:17)
#43     compileToKernel (package:vm/kernel_front_end.dart:320:41)
#44     FrontendCompiler.compile.<anonymous closure> (package:frontend_server/frontend_server.dart:446:54)
#45     new Future.<anonymous closure> (dart:async/future.dart:176:37)
#46     _rootRun (dart:async/zone.dart:1122:38)
#47     _CustomZone.run (dart:async/zone.dart:1023:19)
#48     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
#49     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#50     _rootRun (dart:async/zone.dart:1126:13)
#51     _CustomZone.run (dart:async/zone.dart:1023:19)
#52     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
#53     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
#54     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
#55     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
#56     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)


#0      DietListener.buildFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:946:7)
#1      DietListener.endTopLevelMethod (package:front_end/src/fasta/source/diet_listener.dart:352:5)
#2      Parser.parseTopLevelMethod (package:_fe_analyzer_shared/src/parser/parser_impl.dart:2518:14)
#3      Parser.parseTopLevelMemberImpl (package:_fe_analyzer_shared/src/parser/parser_impl.dart:2372:14)
#4      Parser.parseTopLevelDeclarationImpl (package:_fe_analyzer_shared/src/parser/parser_impl.dart:495:14)
#5      Parser.parseUnit (package:_fe_analyzer_shared/src/parser/parser_impl.dart:352:15)
#6      SourceLoader.buildBody (package:front_end/src/fasta/source/source_loader.dart:329:14)
<asynchronous suspension>
#7      Loader.buildBodies (package:front_end/src/fasta/loader.dart:243:15)
<asynchronous suspension>
#8      KernelTarget.buildComponent.<anonymous closure> (package:front_end/src/fasta/kernel/kernel_target.dart:307:20)
#9      withCrashReporting (package:front_end/src/fasta/crash.dart:122:24)
#10     KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:305:12)
#11     generateKernelInternal.<anonymous closure> (package:front_end/src/kernel_generator_impl.dart:149:38)
<asynchronous suspension>
#12     withCrashReporting (package:front_end/src/fasta/crash.dart:122:24)
#13     generateKernelInternal (package:front_end/src/kernel_generator_impl.dart:70:10)
#14     kernelForProgramInternal.<anonymous closure> (package:front_end/src/api_prototype/kernel_generator.dart:61:35)
#15     CompilerContext.runWithOptions.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:135:20)
<asynchronous suspension>
#16     CompilerContext.runInContext.<anonymous closure>.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:123:46)
#17     new Future.sync (dart:async/future.dart:224:31)
#18     CompilerContext.runInContext.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:123:19)
#19     _rootRun (dart:async/zone.dart:1126:13)
#20     _CustomZone.run (dart:async/zone.dart:1023:19)
#21     _runZoned (dart:async/zone.dart:1518:10)
#22     runZoned (dart:async/zone.dart:1465:12)
#23     CompilerContext.runInContext (package:front_end/src/fasta/compiler_context.dart:122:12)
#24     CompilerContext.runWithOptions (package:front_end/src/fasta/compiler_context.dart:133:10)
#25     kernelForProgramInternal (package:front_end/src/api_prototype/kernel_generator.dart:60:32)
#26     kernelForProgram (package:front_end/src/api_prototype/kernel_generator.dart:52:17)
#27     compileToKernel (package:vm/kernel_front_end.dart:320:41)
#28     FrontendCompiler.compile.<anonymous closure> (package:frontend_server/frontend_server.dart:446:54)
#29     new Future.<anonymous closure> (dart:async/future.dart:176:37)
#30     _rootRun (dart:async/zone.dart:1122:38)
#31     _CustomZone.run (dart:async/zone.dart:1023:19)
#32     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
#33     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#34     _rootRun (dart:async/zone.dart:1126:13)
#35     _CustomZone.run (dart:async/zone.dart:1023:19)
#36     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
#37     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
#38     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
#39     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
#40     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

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

FAILURE: Build failed with an exception.

* Where:
Script '/Users/danielcardona/developer/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 792

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/Users/danielcardona/developer/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 10s
Finished with error: Gradle task assembleDebug failed with exit code 1

我用来为网络选择图像的代码

import 'package:universal_html/prefer_sdk/html.dart' as html;

startFilePickerPc() {
  html.InputElement uploadInput = html.FileUploadInputElement();
  uploadInput.click();

  uploadInput.onChange.listen((e) {
    // read file content as dataURL
    final files = uploadInput.files;
    if (files.length == 1) {
      final file = files[0];

      final reader = new html.FileReader();
      reader.onLoadEnd.listen((e) {
        print(reader.result);
        //  _handleResult(reader.result);
      });

      reader.readAsDataUrl(file);
    }
  });
}

有没有FileReader()的替代方法,或者有没有什么方法可以让它在不需要注释web服务的情况下,为移动设备编译?

非常感谢您的任何帮助。

共有2个答案

慕佑运
2023-03-14

Smyk的回答确实激励了我;-)。

因为我不想要这里所示的所有样板代码,所以我使用了这里解释的实现Dart 2条件导入,最后我这样使用:

导入" package:ptf _ online/utils/file picker _ PC . dart " if(dart . library . io)" package:ptf _ online/utils/file picker _ device . dart ";

我在哪里有我的类startFilePicker()为每个平台。

闻人和歌
2023-03-14

您可以将特定于平台的代码包装在一个通用接口中,但可以将特定平台的实现提取出来以分离.dart文件。然后,您可以使用条件导入将相关实现包含到构建中。

看到这个SO答案获得灵感。

 类似资料:
  • 我成功地编写了一个带有点精灵的标准基本变换反馈粒子系统。没有闪烁,粒子从一个缓冲区更新到下一个缓冲区,然后进行渲染,然后输出缓冲区在下一次迭代时成为输入缓冲区。所有GPU端,标准转换反馈。精彩的一个大问题是:只有在我不使用gl_PointCoord的情况下,它才有效。在我的点精灵中使用平面颜色效果很好。但我需要gl_PointCoord做任何有意义的事。我所有的着色器,不管是否使用gl_Point

  • 我正试图在JDK7U7下编译一个相对简单的、仅客户端的GWT2.5.0应用程序。

  • 问题内容: 因此,我正在读取“ .txt”文件以使用它来实现Dijkstra的算法,但是每次尝试编译它时,都会出现“ FileReader已在此编译单元中定义”错误,同时突出显示我将其导入到的位置在一开始的时候。但是,如果我将其删除,则当我尝试在文件中读取类型错误时,它将引发构造函数错误。我在这里想念什么? 这是我的代码: 如果需要,我可以提供更多信息,但这是所有错误出现的地方。 问题答案: 您的

  • 在Android Studio的编译过程中,我得到了时间异常错误。我该怎么修好它?

  • 我已经尝试过几次升级flutter,但似乎也没有任何问题: 我最近换了笔记本电脑,并在这个新的笔记本上查看了我的代码的git存储库。有一些问题,但我最终解决了这些问题。唯一的区别是项目名称现在只是,而包名称是-但我认为这不会造成任何问题。 我的猜测是,问题出在我的中的某个地方: