Flutter 飞镖/颤振声音示例中的未来不起作用
开始修补飞镖/颤振,我正在尝试录制和播放音频。此库中提供的示例:使用Futures在Dart中显示异步代码Flutter 飞镖/颤振声音示例中的未来不起作用,flutter,dart,async-await,Flutter,Dart,Async Await,开始修补飞镖/颤振,我正在尝试录制和播放音频。此库中提供的示例:使用Futures在Dart中显示异步代码 Future<String> result = await flutterSound.startRecorder(null); result.then(path) { print('startRecorder: $path'); _recorderSubscription = flutterSound.onRecorderStateChanged.liste
Future<String> result = await flutterSound.startRecorder(null);
result.then(path) {
print('startRecorder: $path');
_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
});
}
未来结果=等待声音。startRecorder(空);
结果。然后(路径){
打印('startRecorder:$path');
_recorderSubscription=flatterSound.onRecorderStateChanged.listen((e){
DateTime date=新的DateTime.From毫秒新纪元(e.currentPosition.toInt());
字符串txt=DateFormat('mm:ss:ss','en_US')。格式(日期);
});
}
然而,这段代码甚至没有在我的系统中编译,所以我想知道我遗漏了什么。为了编译此代码,我必须将其更改为:
Future<String> result = widget._flutterSound.startRecorder(null);
result.then((path) {
print('startRecorder: $path');
var _recorderSubscription = widget._flutterSound.onRecorderStateChanged.listen((e) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
print(date);
});
});
Future result=widget.\u.startRecorder(空);
result.then((路径){
打印('startRecorder:$path');
var\u recorderSubscription=widget.\u.onRecorderStateChanged.listen((e){
DateTime date=新的DateTime.From毫秒新纪元(e.currentPosition.toInt());
打印(日期);
});
});
我错过了什么?你试过这个吗:
Future<String> result() async => flutterSound.startRecorder(null);
Future result()async=>flatterSound.startRecorder(空);
使用futures时:异步并等待:
async和await关键字提供了定义异步函数并使用其结果的声明方式。使用异步和等待时,请记住以下两条基本准则:
- 要定义异步函数,请在函数体之前添加async
- wait关键字仅在async函数中有效