I have below packages in my pubspec.yaml
firebase_core: ^2.2.0
firebase_messaging: ^14.1.0
And in my main.dart
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(const MyApp());
}
This causes my main method to run twice, in 2 isolates. If I remove the FirebaseMessaging.onBackgroundMessage(...)
line, it runs a single isolate.
I see this in vscode callstacks window when I put a break point at runApp(...)
as this gets hit twice.
Without onBackgroundMessage
line
With onBackgroundMessage
line
As a result of this, the firebase onMessage
and other handlers fire twice for each notification and does duplicate work, and since they're in different isolates, its hard to check and avoid it. (example above does not have this part)
I see the build method for MyApp
also runs twice, which I don't want. This likely has a memory and compute cost which I would like to avoid.
I would like to know if this is normal behavior? If so, is there a way to avoid this? i.e is there a way to detect the duplicate isolate and main method can return?
If its not normal behavior, what am I doing wrong?
for completeness, output of flutter --version
Flutter 3.3.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 6928314d50 (3 weeks ago) • 2022-10-25 16:34:41 -0400
Engine • revision 3ad69d7be3
Tools • Dart 2.18.2 • DevTools 2.15.0