Im using both image picker and local auth plugins in a flutter project. when im picking images with image_picker plugin (technically it pauses app lifecycle) , local authentication is required to continue. because i've set as follows
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
authenticateUser();
_navigateToHome();
}
}
and also i've faced this issue when picking suggested phone number in textfields.
is there any solution?
Related
I am using Awesome notifications to display notifications in my app. In the notifications, I have an action button, and when pressing it, the app is supposed to read and write some simple data from/to the memory of the phone using shared preferences. This is supposed to happen in the background without opening the app in the foreground.
I tried the following code:
#pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(ReceivedAction action) async {
print('It works');
print(action.toMap());
final SharedPreferences prefs = await SharedPreferences.getInstance();
List<PinnedFolder> pinnedList = [];
try {
final String? pinnedString = prefs.getString('pinnedKey');
if (pinnedString != null) {
pinnedList = PinnedFolder.decode(pinnedString);
print('PinnedList got from memory, length: ${pinnedList.first.pinnedList.length}');
}
} catch (error) {
debugPrint('Error: couldnt get pinned folders: $error');
}
The "It works" and 'action.toMap()' get printed, but I can't get data from shared preferences. Is it so, that I can't use added packages in #pragma("vm:entry-point") functions? What would be the best way to fix the code? The action doesn't need to happen right after the button press, it can also happen the next time when the app is in the foreground, but so that the button action information is still available.
I found a solution using the package flutter_secure_storage.
const storage = FlutterSecureStorage(); await storage.write(key: my_key, value: my_value);
It seems to be so that only a few packages work in #pragma("vm:entry-point") functions, but this is one of them. I implemented it so that every time the app opens, it gets and handles the data from the secure storage before the app is launched.
I am developing a Flutter App that has sound effects when dragging and dropping some widgets; when I was testing it on localhost web and mobile devices, everything works fine; but once I've deployed it on a remote web server, sounds won't play as they're suppossed to:
Sometimes, it just takes a while to load and then it would play fine.
Sometimes, it takes A LOT, by this I mean, sound would play 10 or 20 seconds after widget was pressed.
Sometimes, it won't play any sound and just show this error on console: ยด
The implementation is just as told on the documentation, already tested with local assets and with URL sounds too.
How can I make it work fine on web ?
RELEVANT CODE
import 'package:audioplayers/audioplayers.dart';
class _ResultsScreenState extends State<ResultsScreen>
with SingleTickerProviderStateMixin {
final player = AudioPlayer();
#override
Widget build(BuildContext context) {
player.setReleaseMode(ReleaseMode.stop);
playDrums();
}
playDrums() async {
drumsArePlaying = true;
await player.play(AssetSource('sounds/drumroll.wav'));
}
}
When we show Interstitial ads, App open ad is also displayed each time. it means display twice ads 1 interstitial and another one is App open. some times it going to in infinite loop, App open ads not close.
The problem is interstitial popping up is considered as native android element and it causes flutter app to change its lifecycle to inactive and after you close the native view interstitial the flutter app come back to life and it changes its lifecycle to resumed so your code calls the show app open ads immediately.
As a work around for this ill suggest you to use this plugin : flutter_fgbg
https://pub.dev/packages/flutter_fgbg
and then you can listen to the app if its on background or foreground and show the app open ad when app on foreground
late StreamSubscription subscription;
#override
void initState() {
subscription = FGBGEvents.stream.listen((event) {
print(event);
if (event == FGBGType.foreground) {
MobileAds.instance.initialize();
load();
}
});}
The problem is interstitial popping up is considered as native android element and it causes flutter app to change its lifecycle to inactive and after you close the native view interstitial the flutter app come back to life and it changes its lifecycle to resumed so your code calls the show app open ads immediately.
As a work around for this ill suggest you to use this plugin : flutter_fgbg https://pub.dev/packages/flutter_fgbg
and then you can listen to the app if its on background or foreground and show the app open ad when app on foreground
late StreamSubscription subscription;
#override
void initState() {
subscription = FGBGEvents.stream.listen((event) {
print(event);
if (event == FGBGType.foreground) {
MobileAds.instance.initialize();
load(); }
});}
Currently I am doing a project that calls a secondary app and I launch the 2nd app through InAppWebView. While the 2nd app is facing the user, the inappwebview is still running and sends live update, which I am capturing to trigger events, for example like it will send payment complete, allowing me to trigger to go summary page.
The issue I am having is, I am unable to smoothly transit back to my app. So there are two ways that I found that allow me to go back to the app.
One, launch the URL using my dynamic link but this method I am unable to receive the link.
Second, to launch it through external application like chrome.
The second method works perfectly other than the fact that the screen jumps to chrome. And it continues from where I left off.
However, what I want is both, continue from where I left off and not have the jumping of the chrome. Or from what I have read the right way is to direct to the activity using intent filter. But the smooth method I am not receiving the links but the jump method I am receiving the links.
Please help T - T
if you need to add dynamic links with firebase you can find the setup right in the docs and with the help with firebase_dynamic_links you can handle where to go inside your app and also can execute parameters from the link in the function below you make a stream that listen to any dynamic link clicked while the app in the background or foreground , the PendingDynamicLinkData is when the app terminated. the business logic and how the app interacts ups to you.
static Future<void> initDynamicLink() async {
FirebaseDynamicLinks.instance.onLink.listen((dynamicLink) async {
if (dynamicLink.link != Uri.parse("uri")) {
log(dynamicLink.link.queryParameters.toString());
final Uri deepLink = dynamicLink.link;
// you should handle the navigation or you logic here
}
}).onError((error) {
debugPrint("error from dynamic link Stream : : :: ${error.toString()}");
});
final PendingDynamicLinkData? initialLink =
await FirebaseDynamicLinks.instance.getInitialLink();
try {
if (initialLink != null) {
log(initialLink.link.toString());
final Uri deepLink = dynamicLink.link;
// you should handle the navigation or you logic here
}
} catch (e) {
debugPrint('No deepLink found');
}
}
don't forget to call this function in the main like this
void main()async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
initDynamicLink();
}
I use a function that checks MediaQuery.of(context).platformBrightness to determine the platform dark-mode setting:
This is called from the didChangeAppLifecycleState() and during startup - and it mostly works, except that I find that I need to minimise and re-open the app for it to receive the updated display mode.
The MediaQuery platformBrightness value appears to not be updated when the app comes to the foreground, but if you just minimise and bring the app back to forground, it is correct. This is on the Pie emulator using SDK 29.0.2.
Is there a way to directly listen for notifications about platform dark mode?
Note: There are a number of reasons why I don't use the Flutter Theme engine - The app themes are more complex than what Flutter provides and the user is given a preference setting to override the dark mode / normal setting to force the app to go use dark mode or normal mode and even provide dark and normal mode to older versions of android. However when the user selects "Auto" for dark/normal display mode the app should follow the platform mode, and in fact should switch even if th user does not actually leave the app, such as by just pulling down the shade and setting dark mode on or off. For this I would need a way to listen to the events.
UPDATE:
Futher testing shows the the MediaQuery is in fact updated, but that the value is still reflecting the old value when didChangeAppLifecycleState() is called.
For example the following works:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(
if (displayModePreferenceSetting == DISPLAY_MODE_AUTO) {
Timer(Duration(milliseconds: 333), () {
switchDisplayMode(DISPLAY_MODE_AUTO);
});
}
}
It's much simpler than I thought, though I had to find the answer by accident.
A State widget allows you to override a method didChangePlatformBrightness()
Example:
#override
void didChangePlatformBrightness() {
if (MediaQuery.of(context).platformBrightness == Brightness.dark) {
print('PROBED DISPLAY MODE: DARK');
// DO DARK MAGIC HERE
} else {
print('PROBED DISPLAY MODE: NORMAL');
// NORMAL MODE HERE
}
}