简体   繁体   English

Flutter:断言失败:'file.absolute.existsSync()':不正确

[英]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:伪代码:

  1. User records audio用户录制音频
  2. Audio file is processed using FFMPEG and the new processed file is created on the user's phone使用 FFMPEG 处理音频文件,并在用户手机上创建新的处理文件
  3. User hits submit, uploading the file to Cloud Storage and, if successful, writing a record to Realtime Database用户点击提交,将文件上传到 Cloud Storage,如果成功,将记录写入实时数据库

Order of Functions After User Hits Submit:用户点击提交后的功能顺序:

  1. msgInput.dart -> sendMyAppFile() msgInput.dart -> sendMyAppFile()
  2. msgInput.dart -> prepareMyAppFileForSending() msgInput.dart -> prepareMyAppFileForSending()
  3. msgInput.dart -> runFFMPEGHighLow() msgInput.dart -> runFFMPEGHighLow()
  4. message_dao.dart -> sendMyAppFile() message_dao.dart -> sendMyAppFile()
  5. message_dao.dart -> saveMyAppFileToCloudStorage() //ERROR COMES FROM THIS FUNCTION message_dao.dart -> saveMyAppFileToCloudStorage() //错误来自这个 FUNCTION

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.

相关问题 未处理的异常:“package:firebase_storage/src/storage_reference.dart”:断言失败:第 62 行 pos 12:“file.existsSync()”:不是真的 - Unhandled Exception: 'package:firebase_storage/src/storage_reference.dart': Failed assertion: line 62 pos 12: 'file.existsSync()': is not true Flutter 错误 - 断言失败:第 213 行 pos 15:'data:= null':在从 firestore 获取数据时不正确 - Flutter error - failed assertion: line 213 pos 15: 'data != null': is not true at the time of fetching data from firestore 断言失败:第 13 行第 16 行:'map['saleVal'] != is not true - Failed assertion: line 13 pos 16: 'map['saleVal'] != is not true 未处理的异常:断言失败:第 2609 行 pos 20:'_debugCurrentBuildTarget == context':不是真的 - Unhandled Exception: Failed assertion: line 2609 pos 20: '_debugCurrentBuildTarget == context': is not true 'firebase/main.dart':断言失败 - 'firebase/main.dart': Failed assertion flutter版本解决失败 - version solving failed in flutter 使用 Jest 进行单元测试时出现“FIRESTORE 内部断言失败:意外状态” - 'FIRESTORE INTERNAL ASSERTION FAILED: Unexpected state' when unit testing with Jest 断言失败:检测到冲突的目录结构。 可疑路径 - assertion failed: conflicting directory structures detected. Suspicious paths 断言失败:第 677 行第 11 行:'field == orders[0][0]' - Failed assertion: line 677 pos 11: 'field == orders[0][0]' 带有 firebase 链接的 flutter share_plus - 打开失败:ENOENT(没有这样的文件或目录),null,null,null) - flutter share_plus with firebase link - open failed: ENOENT (No such file or directory), null, null, null)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM