radarlabs / flutter-radar

Flutter package for Radar, the leading geofencing and location tracking platform
https://radar.io
Apache License 2.0
22 stars 13 forks source link

Radar.onEvents() never called #26

Closed MapleNoise closed 1 year ago

MapleNoise commented 1 year ago

I have a problem with Radar.onEvents(), is never called :

Radar.initialize("prj_test_pk_*");
Radar.setUserId(mUserId);
Radar.startTracking('responsive');
Radar.onEvents((result) {
    print("RADAR GEOFENCE DETECTED EVENTS");
    print(result);
});

On my IDE logs I've :

Capture d’écran 2023-01-18 aΜ€ 16 17 28

But my print on the onEvents listener not show.

What have I done wrong ?

Thanks.

brettguenther commented 1 year ago

@MapleNoise - this should be fixed https://github.com/radarlabs/flutter-radar/releases/tag/3.1.5. let us know if you experience any other issues.

Jonny1987 commented 7 months ago

I am having the same problem. I have the following code as the docs state, but the print line inside onEvents never gets executed (but Radar's log line shows the event has been received):

Future<void> startTracking() async {
  Radar.setUserId(authRepository.currentUserId);
  Radar.onEvents(onEvents);
  await Radar.startTracking('continuous');
}

@pragma('vm:entry-point')
static onEvents(Map result) {
  print('event received');
}
KennyHuRadar commented 7 months ago

I noticed that in the provided code snippet, you did not call Radar.attachListeners(); can you try adding that line as such

Radar.setUserId(authRepository.currentUserId);
Radar.attachListeners();
Radar.onEvents(onEvents);
await Radar.startTracking('continuous');
Jonny1987 commented 7 months ago

Thanks, but this is not working either. Just to explain how I call my startTracking function in case it makes a difference....it is inside a class which has a riverpod provider, and then in the initState of a widget I am calling ref.read(radarServiceProvider).startTracking();

Btw, attachListeners is not mentioned on the flutter docs page so you may want to add that (https://radar.com/documentation/sdk/flutter)

KennyHuRadar commented 7 months ago

That is interesting, can you possibly share more code snippets of the way you are managing your state to aid us in reproducing the issue you are encountering?

Jonny1987 commented 7 months ago

Sure...I have the following class:

import 'package:flutter_radar/flutter_radar.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:workwith/src/features/auth/data/auth_repository.dart';

class RadarService {
  final AuthRepository authRepository;
  RadarService({required this.authRepository});

  Future<void> startTracking() async {
    Radar.setUserId(authRepository.currentUserId);
    Radar.attachListeners();
    Radar.onEvents(onEvents);
    await Radar.startTracking('continuous');
  }

  @pragma('vm:entry-point')
  static onEvents(Map result) {
    print('event received');
  }
}

final radarServiceProvider = Provider<RadarService>((ref) {
  final authRepository = ref.watch(authRepositoryProvider);
  return RadarService(authRepository: authRepository);
});

Then in one of my widgets I have this:

  @override
  void initState() {
    super.initState();
    ref.read(radarServiceProvider).startTracking();
  }

I see events coming in from Radars own log lines, but my log line isn't being printed

DigiDevZ commented 4 months ago

Seeing this issue currently in a example project I've spun up to test out Radar. flutter_radar version is 3.9.1

class RadarWidget extends StatefulWidget {
  const RadarWidget({super.key});

  @override
  State<RadarWidget> createState() => _RadarWidgetState();
}

class _RadarWidgetState extends State<RadarWidget> {
  @override
  Widget build(BuildContext context) {
    print('Radar widget building');
    return const map.Map();
  }

  @override
  void initState() {
    super.initState();
    print('Radar init state');
    initRadar();
  }

  @pragma('vm:entry-point')
  static void onLocation(Map res) {
    print('πŸ“πŸ“ onLocation: $res');
  }

  @pragma('vm:entry-point')
  static void onClientLocation(Map res) {
    print('πŸ“πŸ“ onClientLocation: $res');
  }

  @pragma('vm:entry-point')
  static void onError(Map res) {
    print('πŸ“πŸ“ onError: $res');
  }

  @pragma('vm:entry-point')
  static void onLog(Map res) {
    print('πŸ“πŸ“ onLog: $res');
  }

  @pragma('vm:entry-point')
  static void onEvents(Map res) {
    print('πŸ“πŸ“ onEvents: $res');
  }

  @pragma('vm:entry-point')
  static void onToken(Map res) {
    print('πŸ“πŸ“ onToken: $res');
  }

  Future<void> initRadar() async {
    Radar.initialize("..."); //Radar api key
    Radar.setLogLevel('info');

    Radar.attachListeners();

    Radar.onLocation(onLocation);
    Radar.onClientLocation(onClientLocation);
    Radar.onError(onError);
    Radar.onEvents(onEvents);
    Radar.onLog(onLog);
    Radar.onToken(onToken);

    final status = await Radar.getPermissionsStatus();
    final requestedStatus = await Radar.requestPermissions(true);

    await Radar.startTracking('continuous');
    final happening = await Radar.mockTracking(
        //(37.53206895196914,-77.42409998968543)
        origin: {
          'latitude': 37.53206895196914,
          'longitude': -77.42409998968543
        },
        //dest: (37.53011080455556, -77.42597953180213)
        destination: {
          'latitude': 37.53011080455556,
          'longitude': -77.42597953180213
        },
        mode: 'foot',
        steps: 10,
        interval: 5);
  }
}

When running this widget, none of my onEvent print lines are triggered even though I can see the RadarLogger events printing. This issue is definitely not fixed, and it's not clear exactly what is occurring that the callbacks are not being triggered.

Edit: It looks like the logging via Radar.attachListener() is really flaky, I've made some changes to the code above and when running the app (debug env) it's now a 50/50 chance as to wether the logs start printing.

Jonny1987 commented 4 months ago

So I realized I actually hadn't followed the setup instructions fully (I copied the code but didn't change it to my API key), so double check that you have done this correctly: https://radar.com/documentation/sdk/flutter

DigiDevZ commented 4 months ago

Just wanted to drop an update, I was able to get this working consistently in my projects now.

In case of future observers to this issue

KennyHuRadar commented 4 months ago

Thank you for raising this, we will seek to improve the reliability of our SDK in one of our our upcoming releases.