0

Im developing a multiplatform app, it works in Android, IOS and web. I have diferent services that I call for each platform, in one of this services I'm using FileReader() to upload a file from the web versión, it works but if I don't comment the FileReader() object, it dont let me compile 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.

Daniel
  • 1,007
  • 1
  • 11
  • 23

2 Answers2

1

You can wrap the platform-specific code in a common interface, but pull out the platform specific implementation to separate .dart files. Then you can use conditional import to include the relevant implementation into your build.

See this SO answer for inspiration.

smyk
  • 346
  • 2
  • 8
0

The answer of Smyk really did inspire me ;-).

Because I dont wanted all that boilerplate code as shown here this SO answer I used the implementation explained here Dart 2 conditional imports, finally i used like this:

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

Where i have my class startFilePicker() for each platform.

Daniel
  • 1,007
  • 1
  • 11
  • 23