How to solve - Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct). Problem
when i tap on notification to navigate to LeadDetailsScreen i got error as below on Navigator context..
Please help me how i can solved this problem.
Below is my Code.
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends BaseViewState<SplashScreen>
implements SplashContractView {
var _log = FimberLog("Splash"); // ignore: unused_field
late SplashContractPresenter<SplashContractView> presenter;
final FirebaseMessaging _fcm = FirebaseMessaging.instance;
String deviceToken = "";
String deviceModel = "";
String deviceName = "";
String deviceOS = "";
String osVersion = "";
Map<String, dynamic> data = {};
String? caseId;
@override
void initState() {
super.initState();
/// This method call when app is working in application to open from.
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
print("notification1111 ${notification!.body}");
print("message1111 2222 ${message.data['caseId']}");
});
/// This method call when app is working in background.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
RemoteNotification? notification = message.notification;
print("notifications2222 $notification");
AndroidNotification? android = message.notification?.android;
if (message.notification != null) {
CaseModel model = CaseModel();
model.caseId = message.data["caseId"];
print(message.data["caseId"]);
print(model.caseId);
print(message.notification!.title);
print(message.notification!.body);
print("message.data22 ${message.data['caseId']}");
print("message.data22 ${message.data['_id']}");
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => LeadDetailsScreen(caseData: model)),
);
}
});
/// This method call when app is killed(Terminated).
killAppNotificationFunction();
_fcm.getToken().then((token) {
deviceToken = token.toString();
print("Device Token:--- $token");
});
presenter = SplashPresenter();
presenter.onAttach(this);
presenter.init();
}
/// Kill(Terminated) app function.
killAppNotificationFunction() async {
RemoteMessage? terminatedMessage =
await FirebaseMessaging.instance.getInitialMessage();
if (terminatedMessage != null) {
// this is a function I created to route to a page
processKillAppPushNotification(message: terminatedMessage);
}
}
void processKillAppPushNotification({RemoteMessage? message}) async {
var notificationType;
print("message data ${message!.data}");
print("Processing Future after 46 seconds data ${message.data}");
RemoteNotification? notification = message.notification;
print("notification3333 ${notification!.body}");
print("message1111 2222 ${message.data['caseId']}");
print("notificationType $notificationType");
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
print("notification Data");
print("notification Type $notificationType");
CaseModel model = CaseModel();
model.caseId = message.data["caseId"];
print(message.data["caseId"]);
print(model.caseId);
print(message.notification!.title);
print(message.notification!.body);
print("message.data22 ${message.data['caseId']}");
print("message.data22 ${message.data['_id']}");
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => LeadDetailsScreen(caseData: model)),
);
}
Future.delayed(const Duration(
seconds: 4,
)).then((value) async {});
}
@override
void dispose() {
super.dispose();
presenter.onDetach();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Image.asset("assets/images/1234.gif"),
),
),
);
}
@override
void openDashboard() {
Navigator.of(context).popAndPushNamed(Constants.ROUTE_DASHBOARD);
}
@override
void openLogin() {
Navigator.of(context).popAndPushNamed(Constants.ROUTE_LOGIN);
}
}
