Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Created March 1, 2022 09:36
Show Gist options
  • Save ayoubzulfiqar/ce346f9a1165709286883528a4916fc1 to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/ce346f9a1165709286883528a4916fc1 to your computer and use it in GitHub Desktop.
// accesing and requesting
Future<List<Directory>?>? _externalStorageDirectory; // this will go in future in future builder
// this will go in onPresssed if you wanna use button to request everything
void requestExternalStorageDirectory(StorageDirectory type) {
setState(() {
_externalStorageDirectory = getExternalStorageDirectories(type: type);
});
}
// this will define the directory
Widget _buildDirectories(
BuildContext context, AsyncSnapshot<List<Directory>?> snapshot) {
Text text = const Text('');
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
text = Text("Error: ${snapshot.error}");
} else if (snapshot.hasData) {
final String combined = snapshot.data!
// location of the directory ... in this case it root but in default it application directory
.map((Directory d) => Directory('/storage/emulated/0'))
.join(", ");
text = Text("Path: $combined");
} else {
text = const Text('Path Unavailable');
}
}
return Center(
child: text,
);
}
// widget to show the which path we are in the directory.. but it doesn't show the files inside just directory path we are in
Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
child: Text(
!Platform.isAndroid
? 'External directories are unavailable'
: 'Get External Storage Directories',
),
onPressed: !Platform.isAndroid
? null
: () {
requestExternalStorageDirecotry(
StorageDirectory.music,
);
},
),
),
FutureBuilder<List<Directory>?>(
future: _externalStorageDirectory, builder: _buildDirectories),
],
);
// method for requesting permisions
void requestingPermission() async {
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
}
// pass under the application(android\app\src\main\AndroidManifest.xml) in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment