dint-dev / universal_html

Cross-platform 'dart:html'.
https://pub.dev/packages/universal_html
Apache License 2.0
201 stars 63 forks source link

FileReader() crash when compiling to Android #7

Closed DanielCardona closed 4 years ago

DanielCardona commented 4 years ago

I'm developing a multiplatform app, it works in Android, IOS and on the WEB. I have diferent services that I call depending of the platform, in one of this services I'm using FileReader() to upload a file from the web, it works when building for WEB. But if I don't comment the FileReader() object, to hide to compiler, it dont let me build the Android version, here is the output:

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

The code I'm using to pick image for web:

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);
    }
  });
}

Any alternative to FileReader(), or any way to make it work, without needing to comment the web service, to compile for mobile device?

Thank so much for any help.

terrier989 commented 4 years ago

It sounds more like a bug in the compiler than in 'package:universal_html'?

DanielCardona commented 4 years ago

Thanks @terrier989 , then should I open the Issue in flutter Guihub, or wait to get confirmation?

terrier989 commented 4 years ago

You could try whether upgrading everything helps:

flutter upgrade
flutter pub upgrade
DanielCardona commented 4 years ago

I just tried what you suggest but the result is the same.

DanielCardona commented 4 years ago

I opened an Issue in flutter Guithub: https://github.com/flutter/flutter/issues/47589

DanielCardona commented 4 years ago

I reopen the Issue, as it seems related to universal_html https://github.com/flutter/flutter/issues/47589#issuecomment-568258742.

Thank you.

DanielCardona commented 4 years ago

Finally I used dart2 conditionals imports, it solved the problem, thanks!

import "package:ptf_online/utils/filepicker_pc.dart" if (dart.library.io) "package:ptf_online/utils/filepicker_device.dart";

Bilonik commented 4 years ago

same issue with FIleReader(), i cant compile for android.

import 'package:universal_html/html.dart' as html Future setImage(nuevo) async { final completer = Completer<List>(); final html.InputElement input = html.document.createElement('input'); input ..type = 'file' ..multiple = true ..accept = 'image/*'; input.click(); // onChange doesn't work on mobile safari input.addEventListener('change', (e) async { final List files = input.files; Iterable<Future> resultsFutures = files.map((file) { final reader = html.FileReader(); reader.readAsDataUrl(file); reader.onError.listen((error) => completer.completeError(error)); return reader.onLoad.first.then(() => reader.result as String); }); final results = await Future.wait(resultsFutures); completer.complete(results); }); // need to append on mobile safari html.document.body.append(input); // input.click(); can be here final List images = await completer.future; print(images);
input.remove(); }

Bilonik commented 4 years ago

@DanielCardona can you help me fix it?

DanielCardona commented 4 years ago

Hi @alonhh! Maybe this link help you to make it work: https://stackoverflow.com/questions/59342873/flutter-filereader-crash-when-compiling-to-android/59577702#59577702, if not tell me and tomorrow will try to help you.

terrier989 commented 4 years ago

@DanielCardona @alonhh

I was able to reproduce the bug. It's now fixed in the version 1.1.19.