Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved. 2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. 3 : 4 : import 'dart:io'; 5 : 6 : import 'package:file_picker/file_picker.dart'; 7 : import 'package:flutter/foundation.dart'; 8 : 9 : mixin FileImportMixin { 10 0 : Future<String?> importFile(List<String> ext) async { 11 : FilePickerResult? result; 12 0 : if (defaultTargetPlatform == TargetPlatform.android) { 13 0 : result = await FilePicker.platform.pickFiles( 14 : type: FileType.any, 15 : withData: true, 16 : ); 17 : } else { 18 0 : result = await FilePicker.platform.pickFiles( 19 : type: FileType.custom, 20 : allowedExtensions: ext, 21 : withData: true, 22 : ); 23 : } 24 : 25 : String? content; 26 : if (result != null) { 27 0 : Uint8List? fileBytes = result.files.first.bytes; 28 : if (fileBytes != null) { 29 0 : content = String.fromCharCodes(fileBytes); 30 : } else { 31 0 : final file = File(result.paths.first.toString()); 32 0 : content = file.readAsStringSync(); 33 : } 34 : } 35 : return content; 36 : } 37 : }