我正在开发一个 Flutter 应用程序,我需要将文本文件保存到设备的存储中。我使用 file_picker 包来选择保存位置,并使用permission_handler 包来请求必要的权限。但是,我的应用程序在尝试保存文件时崩溃了。有趣的是,创建了一个文件,但文件大小为 0 字节。详情如下:
我的颤振功能
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>
问题描述
当我尝试使用提供的 Flutter 函数保存文本文件时,应用程序崩溃了。然而,在检查文件时,创建了一个文件,但大小为 0 字节。这是详细流程以及我到目前为止所尝试过的:
观察
我尝试过的
构建gradle
/// .......
minSdkVersion 30
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Flutter SDK:3.19.3