Fatal Exception: NSInternalInconsistencyException Sending a message before the FlutterEngine has been run. -[BackgroundLocatorPlugin sendLocationEvent:]

See original GitHub issue

iOS devices. background_locator: 1.6.0+1-beta After tracking longer time in background at the moment of transition to foreground sometimes app crashes.

Fatal Exception: NSInternalInconsistencyException Sending a message before the FlutterEngine has been run. -[BackgroundLocatorPlugin sendLocationEvent:]

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x18ab2586c __exceptionPreprocess
1  libobjc.A.dylib                0x19fb3ec50 objc_exception_throw
2  CoreFoundation                 0x18aa2b000 -[CFPrefsSearchListSource addManagedSourceForIdentifier:user:]
3  Foundation                     0x18bdbf91c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  Flutter                        0x1060e3c14 (Missing)
5  background_locator             0x105ed0b24 -[BackgroundLocatorPlugin sendLocationEvent:] + 114 (BackgroundLocatorPlugin.m:114)
6  background_locator             0x105ed0904 -[BackgroundLocatorPlugin prepareLocationMap:] + 83 (BackgroundLocatorPlugin.m:83)
7  background_locator             0x105ed0984 -[BackgroundLocatorPlugin locationManager:didUpdateLocations:] + 92 (BackgroundLocatorPlugin.m:92)
8  CoreLocation                   0x190a55cb4 CLClientStopVehicleHeadingUpdates
9  CoreLocation                   0x190a54fec CLClientStopVehicleHeadingUpdates
10 CoreLocation                   0x190a3b670 CLClientInvalidate
11 CoreFoundation                 0x18aaa149c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__
12 CoreFoundation                 0x18aaa06e4 __CFRunLoopDoBlocks
13 CoreFoundation                 0x18aa9aaa0 __CFRunLoopRun
14 CoreFoundation                 0x18aa9a21c CFRunLoopRunSpecific
15 GraphicsServices               0x1a2664784 GSEventRunModal
16 UIKitCore                      0x18d4daee8 -[UIApplication _run]
17 UIKitCore                      0x18d4e075c UIApplicationMain
18 Runner                         0x104f50958 main + 7 (AppDelegate.swift:7)
19 libdyld.dylib                  0x18a75a6b0 start

AppDelegate

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
        if #available(iOS 10.0, *) {
          UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
        }

        UIApplication.shared.setStatusBarHidden(false, with: UIStatusBarAnimation.none)
        // Register the plugins with the AppDelegate
        registerPlugins(self)

        BackgroundLocatorPlugin.setPluginRegistrantCallback(registerPlugins)

        // Sut Google Maps api key
        GMSServices.provideAPIKey("AIzaSyBCk1oweYbhQb2BqK3vRq1_F0mHl29YLi0")
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

func registerPlugins(_ registry: FlutterPluginRegistry) {
//    GeneratedPluginRegistrant.register(with: registry)
    CustomPluginRegistrant.register(with: registry)
}

Where CustomPluginRegistrant class looks like this

import Foundation
import firebase_core
import firebase_crashlytics
import firebase_database
import firebase_messaging
import google_maps_flutter
import geolocator
import package_info
import path_provider
import shared_preferences
import sqflite
import url_launcher
import webview_flutter
import background_fetch
import battery
import device_info
import firebase_auth
import share
import background_locator
import date_formatter_plugin
import location_permissions
import app_settings
import flutter_archive


class CustomPluginRegistrant {
    
    static let dateFormatterPlugin = "DateFormatterPlugin"
    static let firebaseCorePlugin = "FLTFirebaseCorePlugin"
    static let firebaseCrashlytics = "FLTFirebaseCrashlyticsPlugin"
    static let firebaseDatabasePlugin = "FLTFirebaseDatabasePlugin"
    static let firebaseMessagingPlugin = "FLTFirebaseMessagingPlugin"
    static let googleMapsPlugin = "FLTGoogleMapsPlugin"
    static let geolocatorPlugin = "GeolocatorPlugin"
    static let packageInfoPlugin = "FLTPackageInfoPlugin"
    static let pathProviderPlugin = "FLTPathProviderPlugin"
    static let sharedPreferencesPlugin = "FLTSharedPreferencesPlugin"
    static let sqflitePlugin = "SqflitePlugin"
    static let urlLauncherPlugin = "FLTUrlLauncherPlugin"
    static let webViewFlutterPlugin = "FLTWebViewFlutterPlugin"
    static let backgroundFetchPlugin = "BackgroundFetchPlugin"
    static let batteryPlugin = "FLTBatteryPlugin"
    static let deviceInfoPlugin = "FLTDeviceInfoPlugin"
    static let firebaseAuthPlugin = "FLTFirebaseAuthPlugin"
    static let sharePlugin = "FLTSharePlugin"
    static let backgroundLocatorPlugin = "BackgroundLocatorPlugin"
    static let locationPermissionsPlugin = "LocationPermissionsPlugin"
    static let appSettingsPlugin = "AppSettingsPlugin"
    static let flutterArchivePlugin = "FlutterArchivePlugin"

    static func register(with registry: FlutterPluginRegistry) {

        if (registry.hasPlugin(dateFormatterPlugin) == false) {
            DateFormatterPlugin.register(with: registry.registrar(forPlugin: dateFormatterPlugin)!)
        }
        if (registry.hasPlugin(firebaseAuthPlugin) == false) {
           FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: firebaseAuthPlugin)!)
        }
        if (registry.hasPlugin(firebaseCorePlugin) == false) {
            FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: firebaseCorePlugin)!)
        }
        if (registry.hasPlugin(firebaseCrashlytics) == false) {
            FLTFirebaseCrashlyticsPlugin.register(with: registry.registrar(forPlugin: firebaseCrashlytics)!)
        }
        if (registry.hasPlugin(firebaseDatabasePlugin) == false) {
            FLTFirebaseDatabasePlugin.register(with: registry.registrar(forPlugin: firebaseDatabasePlugin)!)
        }
        if (registry.hasPlugin(firebaseMessagingPlugin) == false) {
            FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: firebaseMessagingPlugin)!)
        }
        if (registry.hasPlugin(googleMapsPlugin) == false) {
            FLTGoogleMapsPlugin.register(with: registry.registrar(forPlugin: googleMapsPlugin)!)
        }
        if (registry.hasPlugin(geolocatorPlugin) == false) {
            GeolocatorPlugin.register(with: registry.registrar(forPlugin: geolocatorPlugin)!)
        }
        if (registry.hasPlugin(packageInfoPlugin) == false) {
            FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: packageInfoPlugin)!)
        }
        if (registry.hasPlugin(pathProviderPlugin) == false) {
            FLTPathProviderPlugin.register(with: registry.registrar(forPlugin: pathProviderPlugin)!)
        }
        if (registry.hasPlugin(sharedPreferencesPlugin) == false) {
            FLTSharedPreferencesPlugin.register(with: registry.registrar(forPlugin: sharedPreferencesPlugin)!)
        }
        if (registry.hasPlugin(sqflitePlugin) == false) {
            SqflitePlugin.register(with: registry.registrar(forPlugin: sqflitePlugin)!)
        }
        if (registry.hasPlugin(urlLauncherPlugin) == false) {
            FLTURLLauncherPlugin.register(with: registry.registrar(forPlugin: urlLauncherPlugin)!)
        }
        if (registry.hasPlugin(webViewFlutterPlugin) == false) {
            FLTWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: webViewFlutterPlugin)!)
        }
        if (registry.hasPlugin(backgroundFetchPlugin) == false) {
            BackgroundFetchPlugin.register(with: registry.registrar(forPlugin: backgroundFetchPlugin)!)
        }
        if (registry.hasPlugin(batteryPlugin) == false) {
            FLTBatteryPlugin.register(with: registry.registrar(forPlugin: batteryPlugin)!)
        }
        if (registry.hasPlugin(deviceInfoPlugin) == false) {
            FLTDeviceInfoPlugin.register(with: registry.registrar(forPlugin: deviceInfoPlugin)!)
        }
        if (registry.hasPlugin(sharePlugin) == false) {
            FLTSharePlugin.register(with: registry.registrar(forPlugin: sharePlugin)!)
        }
        if (registry.hasPlugin(backgroundLocatorPlugin) == false) {
            BackgroundLocatorPlugin.register(with: registry.registrar(forPlugin: backgroundLocatorPlugin)!)
        }
        if (registry.hasPlugin(locationPermissionsPlugin) == false) {
            LocationPermissionsPlugin.register(with: registry.registrar(forPlugin: locationPermissionsPlugin)!)
        }
        if (registry.hasPlugin(appSettingsPlugin) == false) {
            AppSettingsPlugin.register(with: registry.registrar(forPlugin: appSettingsPlugin)!)
        }
    }
}

Unfortunatelly this is all I know so far. I’ll update when there’s more datails.

Any ideas on what might be going on here or how to prevent this from happening?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mehdokcommented, Jun 20, 2021

@oligazar Nice workaround, but I believe it’s better to find the real problem (why FlutterEngine has stopped running?). Putting this workaround in the library makes it unrelieble.

1reaction
Wian-TMCcommented, Jun 14, 2021

Please try the latest beta version to see if the problem still exist.

still persists in the beta

Read more comments on GitHub >

github_iconTop Results From Across the Web

NSInternalInconsistencyException Sending a message before ...
Fatal Exception : NSInternalInconsistencyException Sending a message before the FlutterEngine has been run. -[BackgroundLocatorPlugin sendLocationEvent:] #237.
Read more >
firebase - Terminating app due to uncaught exception ...
I'm trying to run flutter app on iOS and I get a white screen with an exception: libc++abi: terminating with uncaught exception of...
Read more >
Contributing to the Flutter engine - Google Git
Android (cross-compiling from Mac or Linux). Run the following steps, from the src directory created in the steps above: Update the Flutter Engine...
Read more >
FlutterEngine Class Reference - Flutter - Dart API docs
Destroy running context for an engine. This method can be used to force the FlutterEngine object to release all resources. After sending this...
Read more >
A Flutter plugin for updating location in background.
Fatal Exception : NSInternalInconsistencyException Sending a message before the FlutterEngine has been run. Fatal Exception: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found