android_document_fetcher 1.0.0
android_document_fetcher: ^1.0.0 copied to clipboard
A fast, paginated Flutter plugin to fetch and sort documents from the Android MediaStore using native background threads.
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
// Import your package files
import 'package:android_document_fetcher/android_document_fetcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _fetcher = AndroidDocumentFetcher();
// Notice this is now a strongly typed List of your custom objects!
List<DocumentFile> _documents = [];
bool _isLoading = false;
String _statusMessage = 'Press the button to fetch documents.';
Future<void> _fetchDocuments() async {
setState(() {
_isLoading = true;
_statusMessage = 'Requesting permission...';
});
// Requesting All Files Access for physical device testing
final status = await Permission.manageExternalStorage.request();
if (status.isGranted) {
setState(() => _statusMessage = 'Fetching files from SQLite...');
try {
final docs = await _fetcher.getDocuments();
setState(() {
_documents = docs;
_statusMessage = 'Found ${docs.length} documents.';
});
} catch (e) {
setState(() => _statusMessage = 'Error: $e');
}
} else {
setState(() => _statusMessage = 'Permission denied by user.');
}
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Document Fetcher Test')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
_statusMessage,
style: const TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
if (_isLoading) const CircularProgressIndicator(),
Expanded(
child: ListView.builder(
itemCount: _documents.length,
itemBuilder: (context, index) {
final doc = _documents[index];
// UI code is now incredibly clean with IDE autocomplete support!
return ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
leading: const Icon(Icons.insert_drive_file, size: 40),
title: Text(
doc.name,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 6.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Path: ${doc.path}'),
const SizedBox(height: 4),
Text('Size: ${doc.sizeKb.toStringAsFixed(2)} KB'),
const SizedBox(height: 4),
Text('Type: ${doc.mimeType}'),
const SizedBox(height: 4),
Text(
'Modified: ${doc.lastModified != null ? doc.lastModified!.toLocal().toString() : 'Unknown'}',
),
],
),
),
isThreeLine: true,
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _isLoading ? null : _fetchDocuments,
child: const Icon(Icons.search),
),
),
);
}
}