失败的断言:第 133 行 pos 12:'file.absolute.existsSync()':不是真的

时间:2021-05-11 06:21:50

标签: firebase flutter firebase-storage

您好,我是初学者,我正在尝试录制音频并将其上传到 firebase,但我遇到了一个错误,可能是该文件不存在。

当前 Flutter Audio Recorder 正在读取音频数据,但是当 Audio Recorder 设置为停止并且上传过程开始时,就会产生错误。我已经使用返回 false 的 existsSync() 方法检查了文件是否存在

请帮助我解决这个问题。这是我的代码

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:avatar_glow/avatar_glow.dart';
import 'package:flutter_audio_recorder/flutter_audio_recorder.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:path_provider/path_provider.dart';

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {

  bool _isRecording;
  String _convertedText;
  String _fileName;

  String _filePath;
  FlutterAudioRecorder _audioRecorder;

  @override
  void initState() {
    super.initState();
    _isRecording = false;
    _fileName = "";
    _convertedText = "Press the Record button to convert text";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Urdu Speech to Text Converter"),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: AvatarGlow(
        animate: _isRecording,
        glowColor: Theme
            .of(context)
            .primaryColor,
        endRadius: 75.0,
        duration: const Duration(milliseconds: 2000),
        repeatPauseDuration: const Duration(milliseconds: 100),
        repeat: true,
        child: FloatingActionButton(
          onPressed: _onRecordButtonPressed,
          child: Icon(_isRecording ? Icons.mic : Icons.mic_none),
        ),
      ),
      body: SingleChildScrollView(
        reverse: true,
        child: Container(
            padding: const EdgeInsets.fromLTRB(30.0, 30.0, 30.0, 150.0),
            child: Text(_convertedText)
        ),
      ),
    );
  }

  Future<void> _uploadFile() async {
    FirebaseStorage firebaseStorage = FirebaseStorage.instance;

    try {
      await firebaseStorage
          .ref()
          .child(_fileName)
          .putFile(File(_filePath));
    } catch (error) {
      print('Error occured while uplaoding to Firebase ${error.toString()}');
      // Scaffold.of(context).showSnackBar(
      //   SnackBar(
      //     content: Text('Error occured while uplaoding'),
      //   ),
      // );
    }
  }

  Future<void> _onRecordButtonPressed() async {
    if (_isRecording) {
      _audioRecorder.stop();
      _isRecording = false;
      File(_filePath).existsSync() ? _uploadFile() : print("*************** File Doesn't Exists **************");
    } else {
      _isRecording = true;
      await _startRecording();
    }
    setState(() {});
  }

  Future<void> _startRecording() async {
    final bool hasRecordingPermission =
    await FlutterAudioRecorder.hasPermissions;
    if (hasRecordingPermission) {
      Directory directory = await getApplicationDocumentsDirectory();
      String filepath = directory.path +
          '/' +
          DateTime
              .now()
              .millisecondsSinceEpoch
              .toString() +
          '.wav';
      _audioRecorder =
          FlutterAudioRecorder(filepath, audioFormat: AudioFormat.WAV);
      await _audioRecorder.initialized;
      _audioRecorder.start();
      _filePath = filepath;
      _fileName = _filePath.substring(_filePath.lastIndexOf('/') + 1, _filePath.length);
      _convertedText = _fileName;
      setState(() {});
    } else {
      print("*************************** No Permissions *****************************");
    }
  }

}

0 个答案:

没有答案