Flutter url_launcher not working MissingPluginException - flutter

i want to launch google map when tap on button, nothing complicated. i found easy code also from google search. Pasting the code below for reference. i dont know it is the proper way to do.
class MapUtils {
MapUtils._();
static Future<void> openMap(double latitude, double longitude) async {
String googleUrl =
'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could not open the map.';
}
}
}
pubspec.yaml added -> url_launcher: ^6.0.17
i called this -> MapUtils.openMap(-33.2445107, 9.1530103)
Whenever i tap on the button return error as "Unhandled Exception: MissingPluginException(No implementation found for method launch on channel plugins.flutter.io/url_launcher)"
i tried to find solution, what i tried is
Flutter clean, flutter pub get.
Close everything reopen
uninstall app from emulator device.
close emulator and restart.
total computer restart.
it is nothing related url_launcher still i add the google map api to androidmanifest and appdelegate. Enables Api.
Lot of popel are asking about the same error. May be i am missing something todo.

Related

Flutter release app crashing and showing gray screen

I am getting a crash on some android devices when running my Flutter app.
I have been able to debug a release version of the app and this is the only debug output:
W/FlutterJNI(27448): FlutterJNI.loadLibrary called more than once
W/FlutterJNI(27448): FlutterJNI.prefetchDefaultFontManager called more than once
W/FlutterJNI(27448): FlutterJNI.init called more than once
I donĀ“t know where should I begin to look for the reason of the issue.
The device is showing a gray screen and stops working.
Wherever you create the firebaseMessagingBackgroundHandler, annotate it with #pragma('vm:entry-point') like this:
#pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
}
The reason is explained here:
https://github.com/firebase/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart#L46

Badge counter on launcher icon in flutter

I am trying to implement badger count on app launcher icon in flutter for all android based devices. I've tried flutter_app_badger and flutter_dynamic_icon as well but both of them aren't compatible with android. I wan't a unamious solution it's awesome if it works both for android or ios as well.
I am trying to find solutions but there isn't enough data present. Onesignal push notification I am using for the app provides default badge count but it's not in all devices either.
Please help me with the situation.
setLauncherNumber()async{
// set batch number
try {
print('LauncherBadge inside try');
// FlutterAppBadger.updateBadgeCount(10);
await FlutterDynamicIcon.setApplicationIconBadgeNumber(93);
print('LauncherBadge Success');
} catch (e) {
print('LauncherBadge error $e');
}
}
flutter_app_badger and flutter_dynamic_icon arent compatible with all devices
Use this package flutter_app_badger: ^1.5.0
import 'package:flutter_app_badger/flutter_app_badger.dart';
FlutterAppBadger.updateBadgeCount(1);

Flutter folder picker

I have a button on screen, when you click button I need to open some folder picker.
I have searched for package, but I didn't found any package that also support Android and iOS.
Does someone knows some package who will resolve this problem, or something I could use to make solution of picking folder on iOS?
EDIT:
I found way to get directory picker functionality,I didn't test on iOS but in documentation says it works on both (Android and iOS).
String path = await FilePicker.platform.getDirectoryPath();
With this line above you get new screen to select your directory and get a path like result from that Future.
This is solved using file_picker package.
Use the package file_picker.
Easy to use:
FilePickerResult? result = await FilePicker.platform.pickFiles();
if(result != null) {
File file = File(result.files.single.path);
} else {
// Do something else...
}

clear shared preferences from myBackgroundMessageHandler

I want to clear shared preferences when I send a FCM message and app is in background. Inside myBackgroundMessageHandler method I am calling a method to clear them.
static Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) {
clearPreferences();
}
static void clearPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
}
I am getting the following error:
Unhandled Exception: MissingPluginException(No implementation found
for method getAll on channel plugins.flutter.io/shared_preferences)
step 1)
go to Application.kt/Application.java (in my case is kotlin)
step 2)
add these line into Application class (in kotlin)
if (!registry!!.hasPlugin("io.flutter.plugins.sharedpreferences")) {
SharedPreferencesPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));
}
remember import this as well
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin
step 3)
run flutter clean -> flutter get -> uninstall your app
The code is completely fine.
Just restart your emulator. If you didn't do a full restart by closing the emulator and open it up again, this error can propably occur, because it doesnt have the newly added plugins.
Add SharedPreferences.setMockInitialValues({}) to your code before the runApp() function inside the main function of Flutter App.
this fixed the error for me

Selecting image from Gallery or Camera in Flutter

I am trying to give users a method to add a photo from their gallery or their camera but, flutter keeps crashing after the button is pressed.
I've implemented the image picker dependency as well as Dart:io, migrated to Android X but, still no luck. Here is some of my code:
class _PreferencesState extends State<_Preferences>
with TickerProviderStateMixin {
File _image;
getImage(bool isCamera) async {
File image;
if (isCamera) {
image = await ImagePicker.pickImage(source: ImageSource.camera);
} else {
image = await ImagePicker.pickImage(source: ImageSource.gallery);
}
setState(() {
_image = image;
});
}
Then called here:
FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text('Use Camera'),
onPressed: () {
getImage(true);
},
),
_image == null
? Container()
: Image.file(
_image,
height: 300,
width: 300,
),
Every time I call the method I get this error:
Exception has occurred.
PlatformException (PlatformException(error, Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference, null))
I thought I changed the image type from null so it wouldn't be called but, I'm not sure what else to do here.
The crash could be due to camera permission. Image picker plugin has removed the camera permission condition. When we access camera, app asks us for permission, but this condition in not supported anymore. We need to ask the permission manually using permission_handler plugin. Read more details about this change here and see this link on how to add runtime permission using permission_handler. Hope this resolves your issue.
This might not be the best way to do it but if you want to solve the solution more fast and easy you can just clean all your packages and delete the project then build it again. Use the commands below for the whole process:
Navigate to the folder with the pubspec.yaml file and perform the functions below on your terminal:
[user#machine~]$ flutter clean
[user#machine~]$ adb uninstall com.example.myfirst
[user#machine~]$ flutter build apk
[user#machine~]$ flutter pub get
[user#machine~]$ flutter doctor -v
[user#machine~]$ flutter pub upgrade --major-versions
[user#machine~]$ flutter run
When the the android sdk phone starts ensure you allow the app to access your phone files when you click the button to upload images/files from the gallery
N/B: This worked for my app may it might work on yours or not.