shake icon indicating copy to clipboard operation
shake copied to clipboard

App crashing when closing it

Open SwiftyFlow opened this issue 3 years ago • 15 comments

with the following:

class _ProjectsState extends State<Projects> { 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

  • thread num # 9, queue = 'NSOperationQueue 0x10518af00 (QOS: UNSPECIFIED)', stop reason = signal SIGABRT frame #0: 0x00000001bbade9e8 libsystem_kernel.dylib__pthread_kill + 8 libsystem_kernel.dylib__pthread_kill: -> 0x1bbade9e8 <+8>: b.lo 0x1bbadea04 ; <+36> 0x1bbade9ec <+12>: stp x29, x30, [sp, #-0x10]! 0x1bbade9f0 <+16>: mov x29, sp 0x1bbade9f4 <+20>: bl 0x1bbada670 ; cerror_nocancel Target 0: (Runner) stopped.

Thanks!

SwiftyFlow avatar Feb 24 '22 11:02 SwiftyFlow

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

JTorrus avatar Feb 27 '22 03:02 JTorrus

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

SwiftyFlow avatar Mar 01 '22 09:03 SwiftyFlow

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.

JTorrus avatar Mar 01 '22 13:03 JTorrus

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)

diegoveloper avatar Mar 09 '22 14:03 diegoveloper

Taking a look into this, apologies for the delay.

deven98 avatar Mar 09 '22 14:03 deven98

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

diegoveloper avatar Mar 09 '22 14:03 diegoveloper

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

tarunt815 avatar Mar 09 '22 22:03 tarunt815

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

estien avatar Mar 16 '22 08:03 estien

Is this fixed?

jpetro416 avatar May 06 '22 00:05 jpetro416

@jpetro416 No, this is still an open issue

maxlapides avatar May 06 '22 00:05 maxlapides

@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?

jpetro416 avatar May 06 '22 00:05 jpetro416

@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 ¯_(ツ)_/¯

maxlapides avatar May 06 '22 00:05 maxlapides

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

jpetro416 avatar May 06 '22 13:05 jpetro416

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.

deven98 avatar May 06 '22 15:05 deven98

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

maxlapides avatar Aug 04 '22 18:08 maxlapides

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

deven98 avatar Jan 03 '23 08:01 deven98