bkonyi / FlutterGeofencing

Rough work for Flutter geofencing plugin
BSD 3-Clause "New" or "Revised" License
337 stars 219 forks source link

Exception when using the plugin #90

Open tchouaffe opened 3 months ago

tchouaffe commented 3 months ago

I have tried to follow the example to the best of my abilities and I getting the following error:

java.lang.IncompatibleClassChangeError: Found interface com.google.android.gms.location.GeofencingClient, but class was expected. I am using the geolocator package already.

Here is my the code:

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:geofencing/geofencing.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:radam/core/geofencing/geofencing_handler.dart';
import 'package:radam/features/home/domain/warning.dart';

class NotificationButton extends ConsumerStatefulWidget {
  const NotificationButton({
    required this.warning,
    super.key,
  });

  final Region region;

  @override
  ConsumerState<NotificationButton> createState() => _NotificationButtonState();
}

class _NotificationButtonState extends ConsumerState<NotificationButton> {
  final List<GeofenceEvent> _triggers = <GeofenceEvent>[
    GeofenceEvent.enter,
    GeofenceEvent.exit
  ];
  final AndroidGeofencingSettings _androidSettings = AndroidGeofencingSettings(
    initialTrigger: <GeofenceEvent>[
      GeofenceEvent.enter,
      GeofenceEvent.exit,
    ],
    loiteringDelay: 0,
    notificationResponsiveness: 0,
  );

  void registerGeofence() async {
    final firstPermission = await Permission.locationWhenInUse.request();
    final secondPermission = await Permission.locationAlways.request();
    if (firstPermission.isGranted && secondPermission.isGranted) {
      await GeofencingManager.registerGeofence(
        GeofenceRegion(
          'mtv',
          widget.region.latitude,
          widget.region.longitude,
          1000,
          _triggers,
          androidSettings: _androidSettings,
        ),
        callback,
      );
      final registeredIds = await GeofencingManager.getRegisteredGeofenceIds();
      debugPrint("registerGeofence -> registeredIds: $registeredIds");
    } else {
      debugPrint(
          "right permissions not set: whenInUser: ${Permission.locationWhenInUse.isGranted}, always: ${Permission.locationAlways.isGranted}");
    }
  }

  void unregisterGeofence() async {
    await GeofencingManager.removeGeofenceById('mtv');
    final registeredIds = await GeofencingManager.getRegisteredGeofenceIds();
    debugPrint("unregisterGeofence -> registeredIds: $registeredIds");
  }

  IconData _icon() {
    return Icons.notifications_none_rounded;
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: registerGeofence,
      icon: Icon(_icon()),
    );
  }
}