0

I am getting a issue I am using image picker and web image picker

Things are working well in web but when i am trying to run it in android it is giving some errors due to web image picker

Error not found dart:html Not found dart:js And all

here i am using image picker

           Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    InkWell(
                      onTap: () async {
                        await getImage();
                      },
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.grey[200],
                          borderRadius: BorderRadius.circular(8),
                          border: Border.all(
                            color: Colors.grey,
                            width: 0.5,
                          ),
                          // boxShadow: const [
                          //   BoxShadow(
                          //     color: Colors.grey,
                          //     blurRadius: 2,
                          //     spreadRadius: 1,
                          //   ),
                          // ],
                        ),
                        // padding: const EdgeInsets.all(16),
                        height: MediaQuery.of(context).size.width * 0.4,
                        width: MediaQuery.of(context).size.width * 0.4,
                        child: _image != null
                            ? kIsWeb
                                ? Image.memory(
                                    _image,
                                    fit: BoxFit.cover,
                                  )
                                : Image.file(
                                    _image,
                                    fit: BoxFit.cover,
                                  )
                            : Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Icon(
                                    Icons.photo,
                                    size: 40,
                                    color: Colors.grey[600],
                                  ),
                                  const SizedBox(height: 8),
                                  Text(
                                    'Upload Your Image',
                                    style: TextStyle(
                                        fontSize: 18,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.grey[600]),
                                  ),
                                ],
                              ),
                      ),
                    ),
                   
                  ],
            ),

it is getImage() method

getImage() async {
    if (kIsWeb) {
      Uint8List? image = await ImagePickerWeb.getImageAsBytes();
      setState(() {
        _image = image;
      });
      print('web');
    } else {
      XFile? image = await imagePicker.pickImage(source: ImageSource.gallery);
      setState(() {
        _image = file.File(image!.path);
      });
    }
  }

It is complete error message

Launching lib/main.dart on M2004J19C in debug mode...
Invalid depfile: /home/themahi19/Jfamosdelivery/JFAMOSDelivery/jfamosdelivery/.dart_tool/flutter_build/c0ec4bfe11c84de97b0557ad244bec17/kernel_snapshot.d
Invalid depfile: /home/themahi19/Jfamosdelivery/JFAMOSDelivery/jfamosdelivery/.dart_tool/flutter_build/c0ec4bfe11c84de97b0557ad244bec17/kernel_snapshot.d
: Error: Not found: 'dart:html'
import 'dart:html' as html;
       ^
: Error: Not found: 'dart:html'
import 'dart:html' as html;
       ^
: Error: Not found: 'dart:html'
import 'dart:html' as html;
       ^

: Error: Not found: 'dart:html'
import 'dart:html' as html;
       ^
: Error: Not found: 'dart:html'
import 'dart:html' as html;
       ^
: Error: Not found: 'dart:html'
import 'dart:html';
       ^

: Error: Not found: 'dart:js'
export 'dart:js' show allowInterop, allowInteropCaptureThis;
^

: Error: Type 'html.File' not found.
  static Future<html.File?> _pickFile(String type) async {
                ^^^^^^^^^
: Error: Type 'html.File' not found.
  Future<List<html.File>?> _pickMultiFiles(String type) async {
              ^^^^^^^^^

: Error: Type 'html.File' not found.
  static Future<html.File?> getImageAsFile() {
                ^^^^^^^^^
: Error: Type 'html.File' not found.
  static Future<List<html.File>?> getMultiImagesAsFile() {
                     ^^^^^^^^^
: Error: Type 'html.File' not found.
  static Future<html.File?> getVideoAsFile() {
                ^^^^^^^^^
: Error: Type 'html.File' not found.

extension FileModifier on html.File {
                          ^^^^^^^^^
: Error: Type 'html.EventListener' not found.
  external ui.VoidCallback addPopStateListener(html.EventListener fn);
                                               ^^^^^^^^^^^^^^^^^^
: Error: Type 'html.Location' not found.
  html.Location get _location => html.window.location;
  ^^^^^^^^^^^^^
: Error: Type 'html.History' not found.
  html.History get _history => html.window.history;
  ^^^^^^^^^^^^

: Error: Type 'html.EventListener' not found.
  void addPopStateListener(html.EventListener fn) {
                           ^^^^^^^^^^^^^^^^^^
: Error: Type 'html.EventListener' not found.
  void removePopStateListener(html.EventListener fn) {
                              ^^^^^^^^^^^^^^^^^^
: Error: Type 'AnchorElement' not found.
final AnchorElement _urlParsingNode = AnchorElement();
      ^^^^^^^^^^^^^
: Error: Type 'Element' not found.
final Element? _baseElement = document.querySelector('base');
      ^^^^^^^

: Error: 'File' isn't a type.
    final completer = Completer<List<html.File>?>();

                                          ^^^^
: Error: Method not found: 'FileUploadInputElement'.
    final input = html.FileUploadInputElement() as html.InputElement;
                       ^^^^^^^^^^^^^^^^^^^^^^
: Error: 'InputElement' isn't a type.
    final input = html.FileUploadInputElement() as html.InputElement;
                                                        ^^^^^^^^^^^^
: Error: 'Event' isn't a type.
    void changeEventListener(html.Event e) {
                                  ^^^^^

: Error: 'File' isn't a type.
      final resultFuture = files.map<Future<html.File>>((file) async {
                                                 ^^^^
: Error: Method not found: 'FileReader'.
        final reader = html.FileReader();

                            ^^^^^^^^^^
: Error: 'Event' isn't a type.
    void cancelledEventListener(html.Event e) {
                                     ^^^^^
: Error: Undefined name 'window'.
      html.window.removeEventListener('focus', cancelledEventListener);
           ^^^^^^
: Error: Undefined name 'window'.
    html.window.addEventListener('focus', cancelledEventListener);
         ^^^^^^
: Error: Undefined name 'document'.
    html.document.body!.append(input);
         ^^^^^^^^
: Error: 'File' isn't a type.
    final completer = Completer<List<html.File>?>();
                                          ^^^^
: Error: Method not found: 'FileUploadInputElement'.
    final input = html.FileUploadInputElement();
                       ^^^^^^^^^^^^^^^^^^^^^^
: Error: 'Event' isn't a type.
    void changeEventListener(html.Event e) {
                                  ^^^^^
: Error: 'File' isn't a type.
      final resultsFutures = files.map<Future<html.File>>((file) async {
                                                   ^^^^
: Error: Method not found: 'FileReader'.
        final reader = html.FileReader();
                            ^^^^^^^^^^
: Error: 'Event' isn't a type.
    void cancelledEventListener(html.Event e) {
                                     ^^^^^
: Error: Undefined name 'window'.
      html.window.removeEventListener('focus', cancelledEventListener);
           ^^^^^^
: Error: Undefined name 'window'.

    html.window.addEventListener('focus', cancelledEventListener);
         ^^^^^^
: Error: Undefined name 'document'.
    html.document.body!.append(input);
         ^^^^^^^^
: Error: Method not found: 'FileReader'.
    final reader = html.FileReader();
                        ^^^^^^^^^^
: Error: Method not found: 'FileUploadInputElement'.
    final input = html.FileUploadInputElement();
                       ^^^^^^^^^^^^^^^^^^^^^^
: Error: Undefined name 'document'.
    html.document.body!.append(input);
         ^^^^^^^^
: Error: Method not found: 'FileReader'.
    final reader = html.FileReader();
                        ^^^^^^^^^^
: Error: Method not found: 'FileUploadInputElement'.
    final input = html.FileUploadInputElement();
                       ^^^^^^^^^^^^^^^^^^^^^^
: Error: Undefined name 'document'.
    html.document.body!.append(input);
         ^^^^^^^^
: Error: Method not found: 'FileReader'.

    final reader = html.FileReader();
                        ^^^^^^^^^^
: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/binding.dart').
    WidgetsBinding.instance!.addPostFrameCallback((_) {
                   ^

: Error: Method not found: 'allowInterop'.
    getPath: allowInterop(strategy.getPath),
             ^^^^^^^^^^^^
: Error: Method not found: 'allowInterop'.
    getState: allowInterop(strategy.getState),
              ^^^^^^^^^^^^
: Error: Method not found: 'allowInterop'.
    addPopStateListener: allowInterop(strategy.addPopStateListener),
                         ^^^^^^^^^^^^
: Error: Method not found: 'allowInterop'.
    prepareExternalUrl: allowInterop(strategy.prepareExternalUrl),
                        ^^^^^^^^^^^^
: Error: Method not found: 'allowInterop'.
    pushState: allowInterop(strategy.pushState),
               ^^^^^^^^^^^^
: Error: Method not found: 'allowInterop'.
    replaceState: allowInterop(strategy.replaceState),
                  ^^^^^^^^^^^^
: Error: Method not found: 'allowInterop'.

    go: allowInterop(strategy.go),
        ^^^^^^^^^^^^
: Error: 'EventListener' isn't a type.

  external ui.VoidCallback addPopStateListener(html.EventListener fn);
                                                    ^^^^^^^^^^^^^
: Error: Undefined name 'window'.
  html.Location get _location => html.window.location;
                                      ^^^^^^
: Error: Undefined name 'window'.
  html.History get _history => html.window.history;
                                    ^^^^^^
: Error: 'EventListener' isn't a type.
  void addPopStateListener(html.EventListener fn) {
                                ^^^^^^^^^^^^^
: Error: Undefined name 'window'.
    html.window.addEventListener('popstate', fn);
         ^^^^^^

: Error: 'EventListener' isn't a type.
  void removePopStateListener(html.EventListener fn) {
                                   ^^^^^^^^^^^^^
: Error: Undefined name 'window'.
    html.window.removeEventListener('popstate', fn);
         ^^^^^^

: Error: 'AnchorElement' isn't a type.
final AnchorElement _urlParsingNode = AnchorElement();
      ^^^^^^^^^^^^^

: Error: Method not found: 'AnchorElement'.
final AnchorElement _urlParsingNode = AnchorElement();
                                      ^^^^^^^^^^^^^
: Error: 'Element' isn't a type.
final Element? _baseElement = document.querySelector('base');
      ^^^^^^^
: Error: Undefined name 'document'.
final Element? _baseElement = document.querySelector('base');
                              ^^^^^^^^
: Error: Method not found: 'webOnlySetPluginHandler'.

    ui.webOnlySetPluginHandler(handleFrameworkMessage);
       ^^^^^^^^^^^^^^^^^^^^^^^

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:34:7)

#1      asFileUri (package:vm/kernel_front_end.dart:652:37)
#2      writeDepfile (package:vm/kernel_front_end.dart:792:21)
<asynchronous suspension>
#3      FrontendCompiler.compile (file:///b/s/w/ir/cache/builder/sdk/pkg/frontend_server/lib/frontend_server.dart:615:9)
<asynchronous suspension>
#4      starter (file:///b/s/w/ir/cache/builder/sdk/pkg/frontend_server/lib/frontend_server.dart:1433:12)
<asynchronous suspension>
#5      main (file:///b/s/w/ir/cache/builder/sdk/pkg/frontend_server/bin/frontend_server_starter.dart:10:14)
<asynchronous suspension>

FAILURE: Build failed with an exception.

* Where:
Script '/home/themahi19/snap/flutter/common/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1156

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/home/themahi19/snap/flutter/common/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 34s
Exception: Gradle task assembleDebug failed with exit code 1
Exited

1 Answer 1

0

Having web imports inside a file will throw errors during Run on mobile. To solve the problem, you can use conditional import or create two files (class/widget), one for mobile and the other for web, and based on the platform you will choose which one to use. For more details check this post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.