2020-09-07 19:06:18 +00:00
import ' dart:async ' ;
import ' package:file_picker/file_picker.dart ' ;
import ' package:file_picker/src/platform_file.dart ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:flutter/services.dart ' ;
import ' file_picker_result.dart ' ;
const MethodChannel _channel = MethodChannel ( ' miguelruivo.flutter.plugins.filepicker ' ) ;
const EventChannel _eventChannel = EventChannel ( ' miguelruivo.flutter.plugins.filepickerevent ' ) ;
/// An implementation of [FilePicker] that uses method channels.
class FilePickerIO extends FilePicker {
static const String _tag = ' MethodChannelFilePicker ' ;
static StreamSubscription _eventSubscription ;
@ override
Future < FilePickerResult > pickFiles ( {
FileType type = FileType . any ,
List < String > allowedExtensions ,
Function ( FilePickerStatus ) onFileLoading ,
2020-09-11 17:01:34 +00:00
bool allowCompression = true ,
2020-09-07 19:06:18 +00:00
bool allowMultiple = false ,
2020-09-11 17:01:34 +00:00
bool withData = false ,
2020-09-07 19:06:18 +00:00
} ) = >
2020-09-11 17:01:34 +00:00
_getPath (
type ,
allowMultiple ,
allowCompression ,
allowedExtensions ,
onFileLoading ,
withData ,
) ;
2020-09-07 19:06:18 +00:00
@ override
Future < bool > clearTemporaryFiles ( ) async = > _channel . invokeMethod < bool > ( ' clear ' ) ;
@ override
2020-09-11 13:53:18 +00:00
Future < String > getDirectoryPath ( ) async {
2020-09-07 19:06:18 +00:00
try {
2020-09-11 13:53:18 +00:00
return await _channel . invokeMethod ( ' dir ' , { } ) ;
2020-09-07 19:06:18 +00:00
} on PlatformException catch ( ex ) {
if ( ex . code = = " unknown_path " ) {
print (
' [ $ _tag ] Could not resolve directory path. Maybe it \' s a protected one or unsupported (such as Downloads folder). If you are on Android, make sure that you are on SDK 21 or above. ' ) ;
}
}
return null ;
}
Future < FilePickerResult > _getPath (
FileType fileType ,
bool allowMultipleSelection ,
bool allowCompression ,
List < String > allowedExtensions ,
Function ( FilePickerStatus ) onFileLoading ,
2020-09-11 17:01:34 +00:00
bool withData ,
2020-09-07 19:06:18 +00:00
) async {
final String type = describeEnum ( fileType ) ;
if ( type ! = ' custom ' & & ( allowedExtensions ? . isNotEmpty ? ? false ) ) {
throw Exception ( ' If you are using a custom extension filter, please use the FileType.custom instead. ' ) ;
}
try {
_eventSubscription ? . cancel ( ) ;
if ( onFileLoading ! = null ) {
_eventSubscription = _eventChannel . receiveBroadcastStream ( ) . listen (
( data ) = > onFileLoading ( ( data as bool ) ? FilePickerStatus . picking : FilePickerStatus . done ) ,
onError: ( error ) = > throw Exception ( error ) ,
) ;
}
2020-09-09 23:52:45 +00:00
final List < Map > result = await _channel . invokeListMethod ( type , {
2020-09-07 19:06:18 +00:00
' allowMultipleSelection ' : allowMultipleSelection ,
' allowedExtensions ' : allowedExtensions ,
' allowCompression ' : allowCompression ,
2020-09-11 17:01:34 +00:00
' withData ' : withData ,
2020-09-07 19:06:18 +00:00
} ) ;
if ( result = = null ) {
return null ;
}
2020-09-09 23:52:45 +00:00
return FilePickerResult ( result . map ( ( file ) = > PlatformFile . fromMap ( file ) ) . toList ( ) ) ;
2020-09-07 19:06:18 +00:00
} on PlatformException catch ( e ) {
print ( ' [ $ _tag ] Platform exception: $ e ' ) ;
rethrow ;
} catch ( e ) {
print ( ' [ $ _tag ] Unsupported operation. Method not found. The exception thrown was: $ e ' ) ;
rethrow ;
}
}
}