Flutter: E/location_permissions: result object is null in Geolocator plugin - flutter

I am using Geolocator plugin in Flutter Project. I have added it Pubspecs.yaml file also added the permission in AndroidManifest file.
Pubspecs.yaml
dependencies:
http: ^0.12.0
flutter:
sdk: flutter
shimmer: ^1.0.1
font_awesome_flutter: ^8.8.1
geocoder: ^0.2.1
geolocator: ^5.3.1
Android Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I am using SDK 28 - compileSdkVersion 28
Here is the code for location access.
String _locationMessage = "";
void _getCurrentLocation() async {
print('location');
final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position);
setState(() {
_locationMessage = "${position.latitude}, ${position.longitude}";
});
}
When I call _getCurrentLocation() on Button Pressed. It is showing dialog box to allow location access in App with three option.
Allow all the time
Keep While-In-Use Access
Keep and Don't Ask Again
After selecting Keep While-In-Use Access it is giving below message in Debug console.
E/location_permissions( 7592): Flutter result object is null.
What am I doing wrong?
Also, I want to know will it ask all the times when we try to access location or it will be one time?
Edit
If I select Allow all the time then my app stop responding. I mean if press the button nothing happen. Also I need to uninstall it from phone and run it again then it start giving the prompt for permission.
Edit 2
One strange thing I found. If location service is stop/not enabled then I am getting null. If I manually enable location then it start giving me location.

I think it has something to do with the package that you are using (geolocator),
I've also faced the same issue, when I was following a lecture from youtube
so I've gone for some other popular package to get location
and it's documentation is very simple.

Related

firebase_performance - Default FirebaseApp is not initialized - FlutterFire

I have a flutter project with Android, iOS and Web platforms enabled, I'm using the following firebase plugins:
firebase_performance: ^0.8.2+1
firebase_database: ^9.0.20
cloud_firestore: ^3.4.1
firebase_core: ^1.20.0
All of them are added usgin FlutterFire. This is my main file:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
initializeDateFormatting('es');
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
.....
}
When i remove firebase_perfomance the app works fine, but when i add this plugin the app return the folliwing error message:
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.testapp.test_performance. Make sure to call FirebaseApp.initializeApp(Context) first.
I haven't google-services.json beacause I understand that it is not necessary if I use flutterfire
I run into same problem today :(
It's weird that seems Firebase App won't auto initialized before the flutter startup, if you setup the firebase plugin with flutterfire cli. And no one mentioned this problem.
Solution:
You can follow the https://firebase.google.com/docs/android/setup to setup the google services plugin,the firebase service seems would initialized to get the performance monitor works in early stage of APP.
I have the same issue with firebase_performance package. I solved the issue by running the following commands again.
dart pub global activate flutterfire_cli
flutterfire configure

MissingPluginException(No implementation found in flutter using geolocator

i am using geolocator in flutter project to get current location but this error come on it.
i added all dependencies in both ios and android files still get this error i dont know why
flutter channel stable, 2.12,
here is my code:
Position position = await Geolocato.getCurrentPosition( desiredAccuracy: LocationAccuracy.high);
here is my error coming which i tested android 10, 8 also, but answer same
Unhandled Exception: MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator)
i am using geolocator plugin here is plugin link :https://pub.dev/packages/geolocator
Maybe someone will need my solution. This happens because GeolocatorPlatform.instance is not initialized. Call _registerPlatformInstance() where you need.
import 'package:geolocator_android/geolocator_android.dart';
import 'package:geolocator_apple/geolocator_apple.dart';
void _registerPlatformInstance() {
if (Platform.isAndroid) {
GeolocatorAndroid.registerWith();
} else if (Platform.isIOS) {
GeolocatorApple.registerWith();
}
}
Running flutter clean and flutter pub get fixed the issue for me. I uninstalled the application, cleared the cache of my device and rebuilt the app. This time there was a popup asking to give location permission to the app.
I gave the access and got the current position of the device (latitude and longitude).
Also make sure you upgrade flutter to 3.0.0 by running flutter upgrade
If you just installed the package (geolocator), just restart the app or even better the phone.
Stop running the app completely and restart the app.
make sure that the app is killed before restart
click stop button, refer below,
Upgrade your flutter
Run on terminal flutter upgrade
or flutter upgrade —-force
Then =>flutter pub outdated
Then =>flutter pub upgrade
After that =>flutter pub upgrade —-major-versions
Finally go to
yourProjectName/android/app/build.gradle
Change:
android {
compileSdkVersion ##
…
…
}
To:
android {
compileSdkVersion 33
…
…
}

Google Fonts package Not working in flutter

The google_fonts package is not working in the final build apk in FLutter, it works fine in the debug mode, but as soon as I build it and install the final apk, It just shows regular font.
It could be because of internet permission issue, google fonts using internet acces and if don't apply the permission in your project it won't work when you install the app.
for this you need to add follow line to file AndroidManifest.xml which is there;
yourappname/android/app/src/debug/AndroidManifest.xml
and the line you will add:
<uses-permission android:name="android.permission.INTERNET"/>
I don't believe it is an internet problem. Most times fonts using Flutter packages contain an error in the Pubspec file.
Check your Pubspec.yaml file. Make sure your google fonts package is Formatted this way
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
google_fonts: ^1.1.0
flutter:
assets:
- google_fonts/ # you only need this line and nothing in the fonts area per https://pub.dev/packages/google_fonts
Make sure you have added the below line in /android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
hello my friend try to check the version first ;)
enter image description here
To use google_fonts you must initialize the license for it.
To do that follow the steps:
Suppose you using this:
fontFamily: GoogleFonts.shadowsIntoLight().fontFamily,
Therefore, you are using shadowsIntoLight font.
To license it, first download the particular font from Google Fonts.
After you download the zip files, extract it.
Make a folder named google_fonts in the root directory of your flutter app file.
=> Now place the .ttf and OFL.txt files (present in the extracted folder) in google_fonts folder that you made previously.
[Note: Only OFL.txt is enough for all your fonts.]
This is how my one looks:
Now go to main.dart file:
=> Add these lines inside the void main() {runApp(MyApp());}:
void main() {
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('google_fonts/OFL.txt');
yield LicenseEntryWithLineBreaks(['google_fonts'], license);
});
runApp(...);
}
and import the followings:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Now close your app and restart it and the release it:
flutter build apk --release
Google fonts uses internet, so you may have to update the dependencies in the manifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Else you can try to make the google font available in the offline by adding it in the assets folder.
And then change the code from
// Online Code
Text(
'This is hammersmithOne from Google Font'
style: GoogleFonts.hammersmithOne(),
),
to
// Offline Code
Text(
'This is hammersmithOne from Google Font',
style: TextStyle(fontFamily: 'hammersmithOne') // This is loaded from assets
),
For futher reference refer this article.

Flutter Image picker is already active, null exception

In my flutter project I am getting exception whenever I am trying to pickup an image either from Camera or Gallery using the image_picker plugin of flutter.
For the very first time it asks for the permission and when I allow the camera it throws
java.lang.NullPointerException: 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
After that, it throws an exception for every subsequent tries
PlatformException(already_active, Image picker is already active,
null)
If I try to choose the camera or gallery even after restarting the app.
var imageSource;
if (source == CAMERA_SOURCE) {
imageSource = ImageSource.camera;
} else {
imageSource = ImageSource.gallery;
}
try {
final file = await ImagePicker.pickImage(source: imageSource);
if (file == null) {
throw Exception('File is not available');
}
Below are the dependencies:
cupertino_icons: ^0.1.2
firebase_auth: ^0.8.1
cloud_firestore: ^0.9.0+1
firebase_core: ^0.3.0+1
firebase_messaging: ^3.0.1
firebase_storage: ^2.0.1
intl_translation: ^0.17.3
http: ^0.12.0+1
xml: ^3.3.1
uuid: ^2.0.0
shared_preferences: ^0.5.1+1
flutter_staggered_grid_view: ^0.2.7
google_sign_in: ^4.0.1
flutter_signin_button: ^0.2.5
image_picker: ^0.5.0+2
mlkit: ^0.9.0
path_provider: ^0.5.0+1
Thanks for your time! I also tried to upgrade my flutter to the latest version.
Finally I was able to resolve it.
I updated all my dependencies and flutter SDK and then I did Flutter clean and it started working..
Thanks all for your time and help
Change in android/build.gradle classpath 'com.android.tools.build:gradle:3.5.4'
This worked for me.
Anyway, I have the same issue when using Image Picker.
I have solved the trouble, it needs to provide permission to access the camera and storage memory on the release mode.
On Android:
Add lines on file src/main/AndroidManifest.xml: inside the manifest tag
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
On IOS:
Add lines on file Info.plist:
<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>
<key>NSCameraUsageDescription</key>
<string>Allow access to camera to capture photos</string>
Good luck, hope it will be useful!
I updated package from 0.5.4+3 to ^0.6.5+2 and that solved my problem.
If you read the changelog, you will see every issue fixes: https://pub.dev/packages/image_picker#-changelog-tab-
If you have channel based code i.e bridge between Native Android and Flutter..
In MainActivity
on Activity Result
..
try adding ..
super.onActivityResult(requestCode, resultCode, data)
Removing and Reinstalling application as stated in the comments is fix the problem.
In my case, I haven't given access to "Files". While connecting USB, give access as shown :
Please add this line in your MainActivity.kt class:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data);

Flutter pick image camera or gallery exception

I have a Flutter project in which I am trying to put MLKit for text OCR.
I have crated a standalone Flutter project which works fine with MLKit. However when I put the same code and dependencies to my existing Flutter project it is not working as expected.
Below are the issues I am getting when trying to choosing the image...
When I click on the gallery to choose the image it throws exception.
final file = await ImagePicker.pickImage(source: imageSource);
if (file == null) {
throw Exception('File is not available');
}
When I am trying to choose camera it asks for the permission for the first time and then app gets closed.... throws " java.lang.NullPointerException: 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"
Once these exceptions thrown every subsequent tries it throws exception "PlatformException(already_active, image picker is already active, null)"
cupertino_icons: ^0.1.2
firebase_auth: ^0.8.0+1
cloud_firestore: ^0.9.0
firebase_core: ^0.3.0
intl_translation: ^0.17.2
firebase_messaging: ^3.0.0
http: ^0.12.0+1
xml: ^3.3.1
firebase_storage: ^2.0.0
uuid: ^1.0.3
shared_preferences: ^0.5.0
flutter_staggered_grid_view: ^0.2.7
google_sign_in: ^4.0.0
flutter_signin_button: ^0.2.5
mlkit: ^0.9.0
path_provider: ^0.5.0+1
image_picker: ^0.5.0+3
I have followed below blog in order to implement MLKit https://medium.com/flutter-community/flutter-text-barcode-scanner-app-with-firebase-ml-kit-103db6b6dad7
Thank you
use image_picker plugin
var imageSource;
if (source == CAMERA_SOURCE) {
imageSource = ImageSource.camera;
} else {
imageSource = ImageSource.gallery;
}
try {
final file = await ImagePicker.pickImage(source: imageSource);
if (file == null) {
throw Exception('File is not available');
}
Try cleaning your app, use Flutter clean command.
Many times I have faced this sort of issues where there were no issue related to code and still something went wrong, but somehow Flutter clean tends to help resolve the issue.
You need to delete the build folder in your project. Once you do it and you start your project again, it will work.
First, the problem could be with the permissions, you need to add permission_handler for accessing the hardware like camera or storage etc.
Secondly try other application for camera other than the default app in our phone.