我正在尝试在 flutter(using dart) 中构建一个文本语音应用程序。运行时出现此错误,但据我所知,代码没有问题。我已经粘贴了下面的完整代码。
我遇到的错误是在调用 onListen 处理程序时。它指出语音转文本包中的错误,甚至不是我的代码。请看下面的图片和错误。
TypeError: result[$item] is not a function
at speech_to_text_web.SpeechToTextPlugin.new.[_onResult] (http://localhost:53227/packages/speech_to_text/speech_to_text_web.dart.lib.js:143:36)
at http://localhost:53227/packages/speech_to_text/speech_to_text_web.dart.lib.js:97:98
at Object._checkAndCall (http://localhost:53227/dart_sdk.js:5242:16)
at Object.dcall (http://localhost:53227/dart_sdk.js:5247:17)
at SpeechRecognition.<anonymous> (http://localhost:53227/dart_sdk.js:100617:100)
import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import 'package:avatar_glow/avatar_glow.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter voice',debugShowCheckedmodeBanner: false,theme: ThemeData(
primarySwatch: Colors.red,visualDensity: VisualDensity.adaptivePlatformDensity,),home: SpeechScreen(),);
}
}
class SpeechScreen extends StatefulWidget {
@override
_SpeechScreenState createState() => _SpeechScreenState();
}
class _SpeechScreenState extends State<SpeechScreen> {
stt.SpeechToText _speech = stt.SpeechToText();
bool _isListening = false;
String _text = 'Press the button and start speaking';
double _confidence = 1.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Confidence: ${(_confidence * 100.0).toStringAsFixed(1)}%'),floatingactionButtonLocation: FloatingactionButtonLocation.centerFloat,floatingactionButton: AvatarGlow(
animate: _isListening,glowColor: Theme.of(context).primaryColor,endRadius: 75.0,duration: const Duration(milliseconds: 2000),repeatPauseDuration: const Duration(milliseconds: 100),repeat: true,child: FloatingactionButton(
onpressed: _listen,child: Icon(_isListening ? Icons.mic : Icons.mic_none),body: SingleChildScrollView(
reverse: true,child: Container(
padding: const EdgeInsets.fromLTRB(30.0,30.0,150.0),child: Text(
_text,style: const TextStyle(
fontSize: 32.0,color: Colors.black,fontWeight: FontWeight.w400,);
}
void _listen() async {
if (!_isListening) {
bool available = await _speech.initialize(
onStatus: (val) => print('onStatus: $val'),onError: (val) => print('onError: $val'),);
if (available) {
setState(() => _isListening = true);
_speech.listen(
onResult: (val) => setState(() {
_text = val.recognizedWords;
if (val.hasConfidenceRating && val.confidence > 0) {
_confidence = val.confidence;
}
}));
}
} else {
setState(() => _isListening = false);
_speech.stop();
}
}
}
错误