Goal I want to creating a background service to listen for registration. And if user successfully logged in then it will redirect to home page widget
Code snippet Here is my current code snippet work with the registration goal but not the navigation
in main.dart:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
setupLocator();
BackgroundService.initializeService();
// PermissionService.requestPermissions();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
//..providers
],
child: Consumer<AuthProvider>(
builder: (context, authProvider, child) {
return ToastificationWrapper(
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My App',
theme: ThemeData.light(),
onGenerateRoute: Routes.generateRoute,
initialRoute: Routes.login,
navigatorKey: navigatorKey,
));
},
),
);
}
}
in background_service.dart:
class BackgroundService {
static final SIPUAHelper _helper = SipHelperManager().getHelper();
static Future<void> startBackgroundService() async {
final service = FlutterBackgroundService();
await service.startService();
}
static void stopBackgroundService() {
final service = FlutterBackgroundService();
service.invoke("stop");
}
static Future<void> initializeService() async {
final service = FlutterBackgroundService();
await service.configure(
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStartForeground,
onBackground: onIosBackground,
),
androidConfiguration: AndroidConfiguration(
autoStart: true,
onStart: onStartForeground,
isForegroundMode: true,
autoStartOnBoot: true,
initialNotificationTitle: 'My Softphone',
initialNotificationContent: 'Online',
foregroundServiceTypes: [
AndroidForegroundType.phoneCall,
AndroidForegroundType.microphone,
AndroidForegroundType.mediaPlayback
],
),
);
}
//....
@pragma('vm:entry-point')
static void onStartForeground(ServiceInstance service) async {
service.on('login').listen((event) async {
// ...
SipUAListenerBackground newListener =
SipUAListenerBackground(service);
_helper.addSipUaHelperListener(newListener);
//...
}
}
}
class SipUAListenerBackground implements SipUaHelperListener{
@override
void registrationStateChanged(RegistrationState state) async {
// if state is Registerd
navigatorKey.currentState?.pushNamed('/home'); <--- currentState is null
}
}
Why i post the not working code snippet Because this is probably the closest code to archive what i want
What i tried
I tried to use Get-It to define a NavigationService class but it didn't work too. It said NavigationService is not registed in Get-It
I tried to use Isolate to create background service instead of relying on flutter background service but i got error: UI actions are only available on root isolate.
I need your help
- Can anyone tell me why the currentState is null in my closest code snippet
- And if you can't then how can we work with navigation in foreground service (I did look it up but nothing seem working for me)
Update
- I need to navigate the application using foreground service and i also need to pass object to the navigation code because my routes need some parameters