miguelpruivo / flutter_file_picker

File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.
MIT License
1.35k stars 675 forks source link

NSRangeException exception thrown while downloading document #1564

Closed GowthamanRavichandran3 closed 3 months ago

GowthamanRavichandran3 commented 4 months ago

Describe the bug While downloading a document to the user-selected location, an NSRangeException exception is thrown.

Platform

Platform OS version Model Name: iPad Air (5th generation), iPad OS version: 17.5.1

How are you picking?

import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: FilePickerDemo(),
    );
  }
}

class FilePickerDemo extends StatefulWidget {
  const FilePickerDemo({super.key});

  @override
  _FilePickerDemoState createState() => _FilePickerDemoState();
}

class _FilePickerDemoState extends State<FilePickerDemo> {
  Directory? pickedDirectory;

  @override
  void initState() {
    super.initState();
    _requestPermissions();
  }

  Future<void> _requestPermissions() async {
    await Permission.storage.request();
  }

  Future<void> _pickFolder() async {
    final String? selectedDirectory = await FilePicker.platform.getDirectoryPath();
    if (selectedDirectory != null) {
      setState(() {
        pickedDirectory = Directory(selectedDirectory);
      });
      const String fileContent = 'This is a sample document content.';
      final String filePath = '$selectedDirectory/sample_document.txt';
      final File file = File(filePath);
      await file.writeAsString(fileContent);

      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('File saved at $filePath')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('File Picker Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: _pickFolder,
              child: const Text('Pick a folder'),
            ),
            if (pickedDirectory != null)
              Text('Picked Directory: ${pickedDirectory!.path}'),
          ],
        ),
      ),
    );
  }
}

Details to reproduce the issue

Error Log

Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: (0x19fe40f20 0x197d46018 0x19fde2298 0x102ba3614 0x1a2987070 0x1a22f62e4 0x1a22f5d88 0x1a234f9ac 0x1a23f3e30 0x1a23f3ab4 0x1a20ce91c 0x1a20ce800 0x1a20cd518 0x1a20dab14 0x1a20dab84 0x1a147cc50 0x1039de7bc 0x1039eed58 0x1039ee90c 0x19fe13710 0x19fe10914 0x19fe0fcd8 0x1e4cc01a8 0x1a244890c 0x1a24fc9d0 0x10246d9d8 0x1c34c1e4c) libc++abi: terminating due to uncaught exception of type NSException “Documents” couldn’t be moved because you don’t have permission to access “tmp”.

Screenshots and/or video image

Flutter Version details [✓] Flutter (Channel stable, 3.19.4, on macOS 13.6.6 22G630 darwin-x64, locale en-GB) • Flutter version 3.19.4 on channel stable at /Users/macbookpro/Downloads/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 68bfaea224 (4 months ago), 2024-03-20 15:36:31 -0700 • Engine revision a5c24f538d • Dart version 3.3.2 • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/macbookpro/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15A240d • CocoaPods version 1.15.2

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.91.1) • VS Code at /Users/macbookpro/Desktop/Visual Studio Code.app/Contents • Flutter extension version 3.92.0

[✓] Connected device (3 available) • SYNCIPADN-31422 (mobile) • 00008103-0014504102D3001E • ios • iOS 17.5.1 21F90 • macOS (desktop) • macos • darwin-x64 • macOS 13.6.6 22G630 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 126.0.6478.183

[✓] Network resources • All expected network resources are available.

• No issues found!

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 7 days with no activity.

github-actions[bot] commented 3 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.