HMS-Core / hms-flutter-plugin

This repo contains all of Flutter HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
287 stars 147 forks source link

huawei location package throwing error while build web #380

Open alamen-pgooja opened 3 months ago

alamen-pgooja commented 3 months ago

Description I'm using the same codebase for mobile and web, and I use the Huawei location library. When I run flutter build web, the build crashes during the compilation of the library. Log:

Target dart2js failed: ProcessException: Process exited abnormally with exit code 1:
../../.pub-cache/hosted/pub.dev/huawei_location-6.12.0+302/lib/src/location/location_request.dart:120:23:
Error: The integer literal 9223372036854775807 can't be represented exactly in JavaScript.
    _expirationTime = 9223372036854775807;

Expected Behavior The build should compile normally.

Current Behavior The build fails during compilation.

Logs

Target dart2js failed: ProcessException: Process exited abnormally with exit code 1:
../../.pub-cache/hosted/pub.dev/huawei_location-6.12.0+302/lib/src/location/location_request.dart:120:23:
Error: The integer literal 9223372036854775807 can't be represented exactly in JavaScript.
    _expirationTime = 9223372036854775807;
                      ^^^^^^^^^^^^^^^^^^^

Environment

megaacheyounes commented 3 months ago

Huawei Flutter Location Library depend on Huawei Android Location SDK, it is intended to be used with Android only and it is not compatible with web applications, as a workaround, consider using conditional import to exclude Huawei Location library from your web build:

this code was not tested

create empty implementation for Web platform

example path: lib/stubs/huawei_location_stub.dart:

class FusedLocationProviderClient {
  // Define the methods and properties used from Huawei Location library.

  Future<void> getLastLocation() async {
    // No implementation for web.
  }
  // Add other methods as needed.
}

use conditional import to exclude real FusedLocationProviderClient for web

example path: lib/location_service_selector.dart:

export 'package:huawei_location/huawei_location.dart'
    if (dart.library.html) 'stubs/huawei_location_stub.dart';

update your code to import from location service selector

import 'package:flutter/material.dart';
import 'location_service_selector.dart';

class LocationScreen extends StatelessWidget {
  LocationScreen({Key? key}) : super(key: key);

  final FusedLocationProviderClient _locationService =
      FusedLocationProviderClient();

  getLastLocation() async {
    final Location location = await _locationService.getLastLocation();
    // ...
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(...);
  }
}