Reputation: 2162
I'm trying to get storage permission from the user. Below is the sample (copy-paste) code. But I'm getting error when I'm trying to request the permission.
D/permissions_handler(12775): No permissions found in manifest for: []22
Code
import 'package:duplicate_file_remover/globals.dart' as globals;
import 'package:duplicate_file_remover/ui/views/homeViews/homeView.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:permission_handler/permission_handler.dart';
class MainDataProvider extends StatefulWidget {
const MainDataProvider({Key? key}) : super(key: key);
@override
_MainDataProviderState createState() => _MainDataProviderState();
}
class _MainDataProviderState extends State<MainDataProvider> {
PermissionStatus _permissionStatus = PermissionStatus.denied;
Future<void> _askStoragePermission() async {
debugPrint(" ---------------- Asking for permission...");
await Permission.manageExternalStorage.request();
if (await Permission.manageExternalStorage.request().isGranted) {
PermissionStatus permissionStatus =
await Permission.manageExternalStorage.status;
setState(() {
_permissionStatus = permissionStatus;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: () async {
await _askStoragePermission();
if (_permissionStatus.isGranted) {
debugPrint(" ---------------- Permission allowed");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeView(),
),
);
} else {
// openAppSettings();
debugPrint(" --------------------- Permission denied");
}
},
child: const Text("Get permission"),
),
),
);
}
}
I'm using permission_handler
(https://pub.dev/packages/permission_handler) package.
I tried this solutions but it is not working.
Upvotes: 10
Views: 17821
Reputation: 29
For Android, I added this to my androidManifest.xml file and it seemed to work:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" /> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Although I found out there might be issues while uploading on Play Store. I am open to better ways of doing this.
Upvotes: 0
Reputation: 529
Underlying problem here could be the storage permission change Android did after v13. So application code will need to support both permission types.
First manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then a code like below handle both flows like Even Cai has shared in their answer.
import 'package:permission_handler/permission_handler.dart';
class PermissionUtil {
static Future<bool> tryPermission(Permission permission) async {
var status = await permission.request();
if (status != null) {
if (status.isGranted) {
print('granted');
return true;
} else if (status.isDenied) {
print('requesting permission');
status = await Permission.manageExternalStorage.request();
if (status.isGranted) {
print('newly granted');
return true;
} else {
print('User denied ${permission.runtimeType} request'); // @fixme generalize
}
} else if (status.isPermanentlyDenied) {
print('permanently denied');
}
}
return false;
}
}
// usage
bool permitted = await PermissionUtil.tryPermission(Permission.storage) || await PermissionUtil.tryPermission(Permission.manageExternalStorage); // for android <13 and 13+
// end
Upvotes: 0
Reputation: 91
I was going through the same issue. It looks like the file_picker handles the STORAGE permission itself. So you don't need to ask this permission separately. Just use the pickFile()
function directly and the plugin will handle the rest.
Hope it helps
Upvotes: 0
Reputation: 11
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
Permission.manageExternalStorage,
].request();
var storage = statuses[Permission.storage];
var manageExternalStorage = statuses[Permission.manageExternalStorage];
if (storage.isGranted || manageExternalStorage.isGranted) {
// do something
}
Upvotes: 1
Reputation: 196
For people how experienced the issue also,
Add this permission in your manifest file under <Manifest tag
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
Upvotes: 3
Reputation: 164
I have the same error, I solved it by creating a new flutter project and copy my code to the new
Upvotes: -2