Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when using the plugin #90

Open
tchouaffe opened this issue May 1, 2024 · 0 comments
Open

Exception when using the plugin #90

tchouaffe opened this issue May 1, 2024 · 0 comments

Comments

@tchouaffe
Copy link

tchouaffe commented May 1, 2024

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()),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant