how to close all notifications when app is closed flutter? - flutter

Does anyone have idea? Whenever I close my app, the notification also will automatically close. I try to using "AppLifecycleState.detached" but still doesn't work. It's didn't call out the function of "notifications.cancelAll()". Does anyone got idea to fix this other than using AppLifecycleState? Below are my code:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
print("State: "+state.toString());
if(state == AppLifecycleState.detached){
print("detached");
notifications.cancelAll();
}
}

Related

How to dispose of an unmounted Widget in Flutter?

I am writing a flutter app and if you already started it once, I dont want the user to see the Intro Screen again
In my MaterialApp:
home: firstStart ? DeviceSelection() : StartScreen()
firstStart is a boolean, if it is the first start, it should start the App with the StartScreen() and if its not, it should go straight to DeviceSelection(). That all works fine, but my problem is the following error:
Error: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
I think it starts StartScreen too, even if firstStart is false, because it has to get its value out of the shared preferences, because I see it pop up shortly sometimes.
I already tried some stuff I found, like writing a dispose method:
#override
void dispose() {
super.dispose();
}
or
if (!mounted) {
Navigator.pop(context);
}
in a method that I call after the screen starts, but it doesnt work either. Any ideas what I could do to get rid of this error? Thanks
mb set a flag, like:
bool lookedIntro = false; // if user look it first
and then use:
#override
void initState() {
// take data flag from database or somewhere else (file)
// and check it
}
and also read about initState() more

How to detect when app is minized in flutter

Is there a way to detect when the app has been minimized? Simply using WidgetsBindingObserver with the paused event doesn't work as it's indistinguishable from when the user turns off the screen / phone locks. Note, I need this to work for both android and ios.
A bit of context of what I'm doing. In the application, I'm running a timer. I want to stop this timer if the user minimizes the app (e.g. uses its phone for something else). If the user, however, turns off the screen/locks it, I want the timer to continue.
I suggest to take a look at this package: is_lock_screen
As the description suggest
Useful for determining whether app entered background due to locking screen or leaving app.
I would try with this:
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive) {
final isLock = await isLockScreen();
if(!isLock){
print('app inactive MINIMIZED!');
}
print('app inactive in lock screen!');
} else if (state == AppLifecycleState.resumed) {
print('app resumed');
}
}

Flutter Google Maps blank on app resume with multiple pages displaying map

I have an issue with showing GoogleMaps in my Flutter app, I've implemented it in a swiper, so each page should show the location with a custom marker, would post a code sample but it's too long so I'll try to explain it as short as I can.
Basically when I open the swiper with detail pages in it, it has a map in the bottom and it shows fine while swiping through the pages, but if I put the app in background and get back to it after few swipes it shows just white container with Google logo but no map.
It happens on physical devices but not on the Emulator.
Found the issue opened here
Also tried suggestions from this question
If anyone has an idea on how to fix it, it would be appreciated
I had this issue but didn't realize it was a reported bug. Trying to track down the problem I tried a brute-force mark-the-whole-UI-dirty on foregrounding to see if might be a redraw issue and it stopped happening. Not elegant but it seems to work. None of our testers have reported the blank Google Maps since. I also found I had to do it once Flutter was ready to build() after foregrounding.
This is in the stateful widget that is the first child of my MultiProvider at the top of the app. The google map is in a Scaffold()->Stack() farther down the graph:
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if(state == AppLifecycleState.resumed) {
setState(() { _refreshAllChildrenAfterWakeup = true; });
}
}
void _rebuildAllChildren(BuildContext context) {
void rebuild(Element el) {
el.markNeedsBuild();
el.visitChildren(rebuild);
}
(context as Element).visitChildren(rebuild);
}
Widget build(BuildContext context) {
if(_refreshAllChildrenAfterWakeup == true) {
_refreshAllChildrenAfterWakeup = false;
_rebuildAllChildren(context);
}
//...
}

Flutter Background Service

Hi I'm building a VoIP App in Flutter. I use background_fetch to run a headless task in order to work even if the app is closed. The listener is working, and a notification is sent. But, as the application is closed, the push notification with wake up the app (so home.dart for example) and I would like the push my call screen widget. I see two solution but I don't know how to do it :
the headless task from background_fetch is independent, so I can't transfer my service call data to my app (main) when the user open it, so the call is lost ...
I try to push the right widget (Router.go(/callscreen)) but it's not working.
What can I do in order to fix this ? Thank !
You are using 2 services in background, flutter-local-notification and background-fetch. It's too much. You can use flutter-local-notification in backgound only. Have a look here.
final newRouteName = "callScreen";//Future onSelectNotification(String payload) async
bool isNewRouteSameAsCurrent = false;
Navigator.popUntil(context, (route) {
if (route.settings.name == newRouteName) {
isNewRouteSameAsCurrent = true;
}
return true;
});
if (!isNewRouteSameAsCurrent) {
Navigator.of(context).push(CallScreen())
}

How to go back to the previous screen and reload all function of the first screen?

I have a problem. How to go back to the previous screen and reload all function of the first screen?
On page 1, I have several functions to load all data from json and calculations.
And when user goes to page 2 and does an insert to the json and when the suer goes back to page 1, reload all the functions on page 1.
I have function like below:
Future<void> CountBalanced() {
ListDetailPaymentTransactions(global.strtransidtrans);
global.doubbalanced = global.inttotalpayment - global.doubtranstotaltrans;
print(global.inttotalpayment - global.doubtranstotaltrans);
}
Currently I found this code:
Navigator.push(context, MaterialPageRoute(builder: (context) => CashPayment())).whenComplete(CountBalanced);
But the issue is when the user goes back to page 1 the functions are running all the time and wont stop.
I noticed it when I tried to print the result.
Is there any way to stop it? And is there any better way to reload all the function on page 1?
For stopping your function when navigation try disposing them
#override
void dispose() {
super.dispose();
}
To reloading the methods
#override
void didUpdateWidget(ClassName oldWidget) {
super.didUpdateWidget(oldWidget);
if (comparision) {
//your initState
}
}
For more information
https://docs.flutter.io/flutter/widgets/State/didUpdateWidget.html