Flutter Image picker is already active, null exception - flutter

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);

Related

how to update flutterfire_web_sdk_version and how to enable 'non-nullable' language feature

When I run my code, then I get this warning:
Flutter Web Bootstrap: Programmatic
WARNING: FlutterFire for Web is explicitly tested against Firebase JS SDK version "8.10.1"
but you are currently specifying "8.6.1" by either the imported Firebase JS SDKs in your web/index.html
file or by providing an override - this may lead to unexpected issues in your application. It is recommended that you change all of the versions of the
Firebase JS SDK version "8.10.1":
If you override the version manually:
change:
<script>window.flutterfire_web_sdk_version = '8.6.1';</script>
to:
<script>window.flutterfire_web_sdk_version = '8.10.1';</script>
If you import the Firebase scripts in index.html, instead allow FlutterFire to manage this for you by removing
any Firebase scripts in your web/index.html file:
e.g. remove: <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
I already tried to find the <script>window.flutterfire_web_sdk_version = '8.6.1';</script> in the index file, but I can't find it. Therefore I couldn't upgrade it.
Does someone know how to fix this warning?
second question:
When I want to use non-nullable, then I get this error:
This requires the 'non-nullable' language feature to be enabled.
I am using sdk: '>=2.6.0 <3.0.0' so it should actually be enabled. Does someone know why it isn't working/enabled?
packages that i am using are (maybe this helps you):
cupertino_icons: ^1.0.2
responsive_builder: ^0.4.2
get_it: ^7.2.0
provider: ^4.1.1
provider_architecture: ^1.1.1+1
firebase_auth: ^3.3.19
firebase_core: ^1.17.1
cloud_firestore: ^3.1.13
I already tried to clean flutter and using Pug get again.
Second question:
For null safety you need to use sdk: '>=2.12.0 <3.0.0'. Here you can find how to enable null safety.

Flutter audioplayers FileNotFoundException: assets/audio/test.mp3

First of all, yes I've searched for answers and tried all the solutions but neither of them did work. I tried removing the "assets/" part
I am receiving this error even tho I think my configuration is correct:
successFailAudioPlayer.play("assets/audio/test.mp3", isLocal: true);
pubspec.yaml
dependencies:
flutter:
sdk: flutter
audioplayers: 0.17.0
flutter:
assets:
- assets/audio/
- assets/audio/test.mp3
Apparently we have to use AudioCache() instead of AudioPlayer() when we are playing audio from your assets.

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

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.

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.

Target of URI doesn't exist 'package:english_words/english_words.dart'

I'm learning to use Flutter using Android Studio. I'm going through step by step doc provided by Flutter. At Step:2 Use an external package I'm getting issue importing the english_words package. I have properly added the package in pubspec.yaml and clicked Packages Get which added the dependency but at the time of importing the package in lib/main.dart it is saying
Target of URI doesn't exist
'package:english_words/english_words.dart'.
I have seen many questions on StackOverflow but none of them helped me. Please help!
For some packages, once you do all the process described by the other answers to this question, you have to close the Android project and open it again. As well as the emulator.
Some packages need the restart, others do not need it.
After adding the package in the pubspec.yaml file, you need to execute the command flutter packages get or click on "Packages Get" in the action ribbon at the top of pubspec.yaml file.
Then the dependency and any transitive dependency will be added to the .packages file.
Check this:
https://flutter.io/using-packages/
Solution (For VSCode):
Run flutter packages get in pubspec.yml
Restart VSCode
I had the same issue and went about it like listed below and it worked.
1. Add the package
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
**english_words: ^3.1.5**
Save the pubspec.yaml file
go to your terminal and run 'flutter packages get'
4.Close your simulator and rebuild your cradle
Run the main.dart again and it should remove the error.
This is what worked for me, all the best.
Make sure that english_words is on the base-line with flutter:
Took me a lot of time, but in summary, just save the file pubspec.yaml will do. The command flutter pub get will be ran once u save it, then u can import the package.
There is no need to restart ur editor etc.
My solution was to add the "english_words: ^3.1.5" the version at the moment that I write this, under the "dependencies:" and before the "Flutter:" argument, without the "+" sign.
Hope that helps
I used this and it worked for me... without + sign and version in double quote ""..
english_words: "^3.1.5"
Make Sure that the alignment of english_words is correct inside your pubspec.yaml as it's "space sensitive" :
dependencies:
english_words: ^3.1.5 //two spaces
also never align it using tabs, I don't know why but it never works, and don't use + sign
There are so many methods to solve this issue, most of them will get solved on the second step,if not please try third method also it will work.
Run flutter packages get in pubsec.yaml file (if you are using VS Code it will be easier).
Restart your IDE.
Check it now, mostly it will work. If not try third step.
you have to repair pub caches
flutter packages pub cache repair
I hope it will work. if all methods are not working try to re install flutter.
my friend solution is so simple only add this code line in file pubspec.yaml
english_words: ^3.1.0
like this
dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
english_words: ^3.1.0
dev_dependencies:
flutter_test:
sdk: flutter
it would work perfectly:)
I faced the same issue but I managed to solve it using the steps below:
add the package as an example english_words: ^3.1.0" in 'pubspec.yaml file.
save file or press ctrl+s
//the Output Console will appear this message
[app name] flutter packages get.
Running "flutter pub get" in first_app..., 2.4s
exit code 0.).
3. check main.dart file, the error will be removed.
For me nothing worked. Things resolved only after
Flutter upgrade
in pubsec.yaml file
For those who are still experiencing this problem, it took me hours to figure out what was wrong. Basically, if all the other answers don't work:
Open the test/.packages file on whatever IDE you are using
Locate the package that is not working and go to that directory on your computer.
Delete the whole package. In my case it was the whole english_words package folder.
In the pubspec.lock and .packages file, delete all instances of that package.
run pub get
In the docs of https://flutter.dev/docs/get-started/codelab there is the following code:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
+ english_words: ^3.1.5
The thing which is causing the error is "+ sign". I just removed it and all worked fine for me.
Delete the plus sign "+" in front of english_words (if you have one)
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
english_words: ^3.1.5
I got the same issue.
It is observed only in version 4.0.0 .For lower versions there is no problem.
So here is what you can do to solve this:
Download code for the dependency from :
https://github.com/filiph/english_words
Unzip it Rename it to
english_words-4.0.0
Copy this folder.
Now go to flutter's SDK folder and navigate to
.pub-cache\hosted\pub.dartlang.org
and paste the folder english_words-4.0.0 here. Add dependency in pubspec.yaml file. Now run flutter pub get command.
You are good to go.
Note : How to find the path of Flutter SDK
All you need to do is to restart IDE(Android studio or VSCode)
Having this error message:
Error: Cannot run with sound null safety, because the following
dependencies don't support null safety:
package:english_words
I had to change this dependency english_words: ^3.1.5 to the latest version english_words: ^4.0.0 and make "pub get":
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
english_words: ^4.0.0
provider: ^6.0.0
in your pubspec.yaml file, instead of:
english_words: ^4.0.0
try:
english_words: '4.0.0'
it worked for me