Flutter App Crashes When Saving Text File Using File Picker and Permission Handler

I am developing a Flutter app where I need to save a text file to the device’s storage. I am using the file_picker package to select the save location and the permission_handler package to request necessary permissions. However, my app crashes when trying to save the file. Interestingly, a file gets created but with 0 bytes. Here are the details:

My Flutter Function

Future<void> saveTextFile() async {
    var storageStatus = await Permission.storage.status;
    var manageStorageStatus = await Permission.manageExternalStorage.status;

if (kDebugMode) {
 print('::::::::::::: Storage status: $storageStatus');
 print('::::::::::::: Manage storage status: $manageStorageStatus');
}

if (!storageStatus.isGranted) {
storageStatus = await Permission.storage.request();
} 

if (storageStatus.isDenied || storageStatus.isPermanentlyDenied) {
  manageStorageStatus = await Permission.manageExternalStorage.request();
}

if (storageStatus.isGranted || manageStorageStatus.isGranted) {
  try {
    // Create some lorem ipsum text
    String loremText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
    // Convert the text to bytes
  final bytes = utf8.encode(loremText);
  String fileName = 'LoremIpsum.txt';

  // Open the file picker for the user to choose the save location
  String? outputFile = await FilePicker.platform.saveFile(
    dialogTitle: 'Save your text file',
    fileName: fileName,
    type: FileType.custom,
    allowedExtensions: ['txt'],
  );

  if (outputFile != null) {
    final file = File(outputFile);
    await file.writeAsBytes(bytes, flush: true);

    // Show a message indicating the file has been saved.
    Get.snackbar('Success', 'Lorem ipsum text has been saved as a text file.');
  } else {
    // User canceled the file save dialog
    Get.snackbar('Cancelled', 'File save operation was cancelled.');
  }
} catch (e) {
  // Catch any errors during the file writing process
  Get.snackbar('Error', 'An error occurred while saving the file: $e');
  debugPrint('Error saving file: $e');
 }
} else {
// Handle permission denial
Get.snackbar('Permission Denied', 'Storage permission is required to save the file.');
}

}

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<application
    android:label="SJIC Admin"
    android:name="${applicationName}"
    android:icon="@mipmap/launcher_icon"
    android:requestLegacyExternalStorage="true">
    
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Problem Description

When I attempt to save a text file using the provided Flutter function, the app crashes. However, upon checking the files, a file is created but it is 0 bytes in size. Here is the detailed flow and what I have tried so far:

  1. Request Permissions: I request storage permissions using the permission_handler package.
  2. Generate Text Content: I generate some lorem ipsum text and convert it to bytes.
  3. File Picker: I use the file_picker package to allow the user to select the save location.
  4. Save File: I attempt to save the file to the chosen location.

Observations

  • The file is created but is 0 bytes.
  • The app crashes without detailed error logs.

What I’ve Tried

build gradle

       /// .......
       minSdkVersion 30
       targetSdkVersion flutter.targetSdkVersion
       versionCode flutterVersionCode.toInteger()
       versionName flutterVersionName
   }

Flutter SDK : 3.19.3

  • path_provider: ^2.1.3
  • permission_handler: ^11.3.1
  • file_picker: ^8.0.3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật