deven98 / shake

A flutter package for detecting phone shakes.
BSD 2-Clause "Simplified" License
92 stars 52 forks source link

App crashing when closing it #12

Closed SwiftyFlow closed 1 year ago

SwiftyFlow commented 2 years ago

with the following:

class _ProjectsState extends State { bool isShowingFavorites = false;

late ShakeDetector detector; <----

@override void initState() { super.initState();

detector = ShakeDetector.waitForStart(onPhoneShake: () { <----
  setState(() {
    isShowingFavorites = !isShowingFavorites;
  });
});
detector.startListening(); <----

}

@override void dispose() { super.dispose(); detector.stopListening(); <---- }

If I close the app, there is a crash in background...

If I remove the ShakeDetector then it's all good.

Any clue why?

Assertion failure in -[FlutterEngine sendOnChannel:message:binaryReply:], FlutterEngine.mm:941 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Sending a message before the FlutterEngine has been run.' *** First throw call stack: (0x182075c9c 0x199229758 0x18384621c 0x10b379d70 0x1048227c8 0x1048226a0 0x18377061c 0x183781b14 0x18375c23c 0x18376c31c 0x18376f714 0x18377ce4c 0x181d23ca0 0x181d42198 0x181d19714 0x181d18e40 0x181d262e0 0x181d26abc 0x1dc041e48 0x1dc0419f0) libc++abi: terminating with uncaught exception of type NSException

Thanks!

JTorrus commented 2 years ago

Got the same exception with Flutter version 2.10.2. Could you find any workaround apart from removing the ShakeDetector?

SwiftyFlow commented 2 years ago

nah just gonna remove it or use another method...

JTorrus commented 2 years ago

I found a way to prevent this crash. Add WidgetsBindingObserver to your Widget and implement didChangeAppLifecycleState(AppLifecycleState state) to handle the ShakeDetector subscription. It would look something like this:

class _MyApp extends State<MyApp> with WidgetsBindingObserver {
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    // Init subscription
    detector.startListening();
  }

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.resumed:
        // Start a new subscription when the app is active again
        detector.startListening();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
        // Cancel current subscription when the app is minimized and/or killed
        detector.stopListening();
        break;
    }
  }

  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

I don't know why the sensors subscription keeps firing events when the engine is detached. Implementing the methods above will properly dispose it in time.

diegoveloper commented 2 years ago

I found a way to prevent this crash. Add WidgetsBindingObserver to your Widget and implement didChangeAppLifecycleState(AppLifecycleState state) to handle the ShakeDetector subscription. It would look something like this:

class _MyApp extends State<MyApp> with WidgetsBindingObserver {
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    // Init subscription
    detector.startListening();
  }

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.resumed:
        // Start a new subscription when the app is active again
        detector.startListening();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
        // Cancel current subscription when the app is minimized and/or killed
        detector.stopListening();
        break;
    }
  }

  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

I don't know why the sensors subscription keeps firing events when the engine is detached. Implementing the methods above will properly dispose it in time.

I tried this workaround but I still see the issue :/ (iOS 14.1)

deven98 commented 2 years ago

Taking a look into this, apologies for the delay.

diegoveloper commented 2 years ago

hey @deven98 , take a look at this thread, maybe it could help :https://github.com/flutter/flutter/issues/96901#issuecomment-1053114107

tarunt815 commented 2 years ago

also having this issue, causing crashes when I try to hard close app on iOS (full close). Noticed a similar thread in the sensors_plus package, maybe related

estien commented 2 years ago

It's most likely the same issue as this one https://github.com/fluttercommunity/plus_plugins/issues/766

jpetro416 commented 2 years ago

Is this fixed?

maxlapides commented 2 years ago

@jpetro416 No, this is still an open issue

jpetro416 commented 2 years ago

@deven98 @maxlapides Okay just wondering, I've seen this crash before but it's not common. Was curious as I'm using it for a cool feature in my production app. Good to know the team is actively on here lol. On a side note, it hasn't been working on Android for some reason, any thoughts as to why that might be the case?

maxlapides commented 2 years ago

@jpetro416 It works for us on Android, but we've noticed that on some Android devices you have to shake it REALLY hard for it work ¯_(ツ)_/¯

jpetro416 commented 2 years ago

@maxlapides ahh, so gravity settings should be different then, cool! thanks for the tip 😎

deven98 commented 2 years ago

There's no obvious reason or fix on this side but I'm still being mindful. Regardless, I'm working on an update for the package which should be out soon with a much-improved version of detecting shakes.

maxlapides commented 1 year ago

I think this issue is now resolved with the latest version of sensors_plus (1.3.4)

deven98 commented 1 year ago

New version should fix this issue (was a sensors_plus issue): https://pub.dev/packages/shake