You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dart中无需Method Channel申请WRITE_EXTERNAL_STORAGE权限?

How to Request WRITE_EXTERNAL_STORAGE Permission in Dart (No Method Channel/Java Code)

Hey there! I get you want to handle storage permissions purely in Dart without touching Method Channels or writing native Java code—let's break this down with a straightforward solution using Flutter's go-to permission handling package.

Step 1: Add the permission_handler Dependency

This package abstracts all native permission logic behind a clean Dart API, so you don't need to write any Java/Kotlin code yourself. Add it to your pubspec.yaml:

dependencies:
  permission_handler: ^10.2.0 # Use the latest version available

Step 2: Confirm Your AndroidManifest Configuration

You already added the permission, but just a quick note for compatibility:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- For Android 10+, if you need to bypass scoped storage, add this too (optional): -->
<application android:requestLegacyExternalStorage="true">
  ...
</application>

Step 3: Implement Permission Request in Dart

Here's a complete function to request storage permission, check its status, and handle different outcomes:

import 'package:permission_handler/permission_handler.dart';

Future<void> requestStoragePermission() async {
  // Request the storage permission
  final PermissionStatus status = await Permission.storage.request();

  switch (status) {
    case PermissionStatus.granted:
      // Permission is granted—proceed with your storage operations
      print("WRITE_EXTERNAL_STORAGE permission granted!");
      break;
    case PermissionStatus.denied:
      // Permission was denied temporarily—you can ask again later
      print("Permission denied. You can request it again.");
      break;
    case PermissionStatus.permanentlyDenied:
      // Permission was permanently denied—direct user to app settings
      print("Permission permanently denied. Please enable it in app settings.");
      await openAppSettings(); // Opens the app's settings page
      break;
    default:
      break;
  }
}

Key Notes for Android Versions

  • Android 13+: The WRITE_EXTERNAL_STORAGE permission is deprecated and split into granular permissions like READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, etc. The permission_handler package automatically handles this—calling Permission.storage will request the appropriate permissions based on the Android version.
  • Scoped Storage: If you're targeting Android 10 (API 29) or higher, consider using scoped storage instead of relying on WRITE_EXTERNAL_STORAGE for better compliance, but the above code still works if you need the legacy access.

This approach keeps everything in Dart, no native code required, and covers all common permission scenarios.

内容的提问来源于stack exchange,提问作者Anil8753

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:12:37