skip to Main Content

enter image description here

I try to code some gallery picker function from Youtube but I want to ask for permission for storage to user ,when I code below code, I get into the app info why I get there and how to fix it

import 'package:photo_manager/photo_manager.dart';

class MediaServices {
  Future loadAlbum(RequestType requestType) async {
    var permission = await PhotoManager.requestPermissionExtend();
    List<AssetPathEntity> albumList = [];
    if (permission == true) {
      albumList = await PhotoManager.getAssetPathList(
        type: requestType,
      );
    } else {
      PhotoManager.openSetting();
    }
    return albumList;
  }
  Future loadAssets(AssetPathEntity selectedAlbum) async {
  // ignore: deprecated_member_use
  List<AssetEntity> assetList = await selectedAlbum.getAssetListRange(
      // ignore: deprecated_member_use
      start: 0, end: selectedAlbum.assetCount);
      return assetList;
}

}

2

Answers


  1. Chosen as BEST ANSWER

    Finally I figured out that I have missed something in my code . And I watched video agian and I realized it.I have to put "if(permission.isAuth==true)" instead of this one "if(permission==true)".

    Instead of this

    import 'package:photo_manager/photo_manager.dart';
    
    class MediaServices {
      Future loadAlbum(RequestType requestType) async {
        var permission = await PhotoManager.requestPermissionExtend();
        List<AssetPathEntity> albumList = [];
        if (permission == true) {
          albumList = await PhotoManager.getAssetPathList(
            type: requestType,
          );
        } else {
          PhotoManager.openSetting();
        }
        return albumList;
      }
      Future loadAssets(AssetPathEntity selectedAlbum) async {
      // ignore: deprecated_member_use
      List<AssetEntity> assetList = await selectedAlbum.getAssetListRange(
          // ignore: deprecated_member_use
          start: 0, end: selectedAlbum.assetCount);
          return assetList;
    }
    

    I have to use this one

    import 'package:photo_manager/photo_manager.dart';
    
    class MediaServices {
      Future loadAlbum(RequestType requestType) async {
        var permission = await PhotoManager.requestPermissionExtend();
        List<AssetPathEntity> albumList = [];
        if (permission.isAuth == true) {
          albumList = await PhotoManager.getAssetPathList(
            type: requestType,
          );
        } else {
          PhotoManager.openSetting();
        }
        return albumList;
      }
      Future loadAssets(AssetPathEntity selectedAlbum) async {
      // ignore: deprecated_member_use
      List<AssetEntity> assetList = await selectedAlbum.getAssetListRange(
          // ignore: deprecated_member_use
          start: 0, end: selectedAlbum.assetCount);
          return assetList;
    }
    

  2. Use Permission handler plugin from pub.dev, this is similar answer

    for android add these lines in Androidmainfest.xml file

    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permissionandroid:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    

    for iOS add below code in info.plist file

    <key>NSMicrophoneUsageDescription</key>
    <string>This app require permission to access microphone</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>This app require permission to access gallery</string>
    

    modify below code accordingly.

     Future<void> getStorageReadPermission() async {
        final storage = await Permission.storage.status;
        !storage.isGranted
            ? await Permission.storage.request()
            : debugPrint("Storage read permission already granted");
      }
    
      Future<void> getStorageManagePermission() async {
        final manageStorage = await Permission.manageExternalStorage.status;
        !manageStorage.isGranted
            ? await Permission.manageExternalStorage.request()
            : debugPrint("storage permission already granted");
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search