首页 > 解决方案 > 构建网络时语音到文本的错误

问题描述

我正在尝试在 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, 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();
    }
  }
}

错误

在此处输入图像描述

标签: flutterandroid-studiodartspeech-recognition

解决方案


推荐阅读