[英]Flutter: Failed assertion: 'file.absolute.existsSync()': is not true
In my app, a user can send a file to others in a group chat.在我的应用程序中,用户可以在群聊中向其他人发送文件。 First, the user records some audio using their mic.首先,用户使用他们的麦克风录制一些音频。 The file is then touched up using FFMPEG.然后使用 FFMPEG 修改该文件。 Then, the file is uploaded to Firebase Cloud Storage and if this is successful, a record is written in Firebase Realtime Database.然后,将文件上传到 Firebase 云存储,如果成功,则会在 Firebase 实时数据库中写入一条记录。
I'm getting the error below when the user records a long audio file and then presses submit.当用户录制长音频文件然后按提交时,我收到以下错误。 It almost seems as though FFMPEG hasn't finished processing the file...but I thought I used my async/await correctly to make sure that this processing is finished before moving on?似乎 FFMPEG 还没有完成文件处理......但我认为我正确使用了我的 async/await 以确保在继续之前完成此处理?
##MyAppFile## saveMyAppFileToCloudStorage Error: 'package:firebase_storage/src/reference.dart': Failed assertion: line 127 pos 12: 'file.absolute.existsSync()': is not true. ##MyAppFile## saveMyAppFileToCloudStorage 错误:'package:firebase_storage/src/reference.dart':断言失败:第 127 行 pos 12:'file.absolute.existsSync()':不正确。
Psuedo-code:伪代码:
Order of Functions After User Hits Submit:用户点击提交后的功能顺序:
The Code:编码:
//msgInput.dart
Future<void> sendMyAppFile() async {
if (sendableMyAppFileExists == 1) {
final MyAppFileReadyToBeSent = await prepareMyAppFileForSending();
if (MyAppFileReadyToBeSent == '1') {
messageDao.sendMyAppFile(MyAppFile, filepath, filename);
} else {
}
}
setState(() {
sendableMyAppFileExists = 0;
});
}
Future<String> prepareMyAppFileForSending() async {
if (sendableMyAppFileExists == 1) {
if (recordedMyAppFileFilterID == '1') {
await runFFMPEGHighLow('1');
return '1';
}
if (recordedMyAppFileFilterID == '2') {
await runFFMPEGHighLow('2');
return '1';
}
}
return '0';
}
Future<void> runFFMPEGHighLow(String filterID) async {
if (filterID != '1' && filterID != '2') {
return;
}
if (sendableMyAppFileExists == 1) {
if (filterID == '1') {
await FFmpegKit.executeAsync(/*...parms...*/);
setState(() {
currentMyAppFileFilename = currentMyAppFileFilename + '1.mp3';
});
}
if (filterID == '2') {
await FFmpegKit.executeAsync(/*...parms...*/);
setState(() {
currentMyAppFileFilename = currentMyAppFileFilename + '2.mp3';
});
}
}
}
//message_dao.dart
void sendMyAppFile(ChatData MyAppFile, String filepath, String filename) {
saveMyAppFileToCloudStorage(filepath, filename).then((value) {
if (value == true) {
saveMyAppFileToRTDB(MyAppFile);
}
});
}
Future<bool> saveMyAppFileToCloudStorage(String filepath, String filename) async {
//filepath: /data/user/0/com.example.MyApp/app_flutter/MyApp/MyAppAudioFiles/MyAppFiles/2d7af6ae-6361-4be5-8209-8498dd17d77d1.mp3
//filename: 2d7af6ae-6361-4be5-8209-8498dd17d77d1.mp3
_firebaseStoragePath = MyAppFileStorageDir + filename;
File file = File(filepath);
try {
await _firebaseStorage
.ref(_firebaseStoragePath)
.putFile(file);
return true;
} catch (e) {
print('##MyAppFile## saveMyAppFileToCloudStorage Error: ' + e.toString()); //ERROR COMES FROM THIS LINE
return false;
}
return true;
}
I assume you're using the package ffmpeg_kit_flutter .我假设您使用的是 package ffmpeg_kit_flutter 。
First, why it's not working: execute and executeAsync return FFmpegSession objects.首先,为什么它不起作用: execute 和 executeAsync 返回FFmpegSession对象。 The run of FFmpeg doesn't need to be finished for these methods to complete. FFmpeg 的运行不需要完成这些方法即可完成。 In fact, the returned session object has methods like getState to monitor whether the run of FFmpeg has completed.其实返回的session object有getState之类的方法来监控FFmpeg的运行是否完成。
A good way to fix this: The documentation for executeAsync has a hint for what to do here.解决此问题的一个好方法: executeAsync 的文档提示了此处的操作。
Note that this method returns immediately and does not wait the execution to complete.请注意,此方法会立即返回,并且不会等待执行完成。 You must use an FFmpegSessionCompleteCallback if you want to be notified about the result.如果您想收到有关结果的通知,则必须使用 FFmpegSessionCompleteCallback。
You can set a completion callback by passing a function to executeAsync.您可以通过将 function 传递给 executeAsync 来设置完成回调。 Here's the full function signature from the docs:这是来自文档的完整 function 签名:
Future<FFmpegSession> executeAsync(
String command,
[FFmpegSessionCompleteCallback? completeCallback = null,
LogCallback? logCallback = null,
StatisticsCallback? statisticsCallback = null]
)
FFmpegSessionCompleteCallback is just a function that accepts an FFmpegSession and returns nothing. FFmpegSessionCompleteCallback只是一个 function 接受一个 FFmpegSession 并且什么都不返回。 You can provide your own.你可以提供你自己的。
void someCompletionFunction() {
setState(() {
currentMyAppFileFilename = currentMyAppFileFilename + '1.mp3';
});
}
await FFmpegKit.executeAsync(/*...parms...*/, someCompletionFunction);
Future vs callback: If you prefer to use Futures and async-await instead of callbacks, you'll need to create your own Future and update it in the callback. Future 与回调:如果您更喜欢使用 Futures 和 async-await 而不是回调,则需要创建自己的 Future 并在回调中更新它。 See Dart, how to create a future to return in your own functions?见Dart,如何在自己的函数中创建返回的未来? for an example.例如。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.