【问题标题】:Flutter : Speech to text error The method 'initialize' was called on nullFlutter:Speech to text 错误在 null 上调用了方法“initialize”
【发布时间】:2020-11-30 12:57:44
【问题描述】:

我正在尝试将语音到文本方法添加到我的应用程序以转换我的 但我有这个错误

[错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常: NoSuchMethodError:在 null 上调用了方法“initialize”。 E/flutter(13534):接收者:空 E/flutter(13534):尝试调用: 初始化(onError:关闭:(SpeechRecognitionError)=> void, onStatus: Closure: (String) => void)

请注意,我将录制权限放入 android manifest 中

这是我用于语音到文本的函数

bool isListening = false;
  stt.SpeechToText _speech;

void listen() async {
    if (!isListening) {
      bool available = await _speech.initialize(
        onStatus: (val) => print('onState: $val'),
        onError: (val) => print('onError: $val'),
      );
      if (available) {
        setState(() => isListening = true);
        _speech.listen(
          onResult: (val) => setState(() {
            resultText = val.recognizedWords;
          }),
        );
      } else {
        setState(() => isListening = false);
        _speech.stop();
      }
    }
  }

这是我的显示代码

Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: <Widget>[
                                            AvatarGlow(
                                              animate: isListening,
                                              glowColor: Colors.red[200],
                                              endRadius: 75.0,
                                              duration: const Duration(
                                                  milliseconds: 2000),
                                              repeatPauseDuration:
                                                  const Duration(
                                                      milliseconds: 100),
                                              repeat: true,
                                              child: FloatingActionButton(
                                                child: Icon(isListening
                                                    ? Icons.mic
                                                    : Icons.mic_none),
                                                onPressed: (){
                                                  listen();
                                                },
                                                mini: true,
                                              ),
                                            ),
                                            Container(
                                              width: MediaQuery.of(context)
                                                      .size
                                                      .width *
                                                  0.4,
                                              decoration: BoxDecoration(
                                                color: Colors.grey[350],
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        10.0),
                                              ),
                                              child: Text(
                                                resultText,
                                              ),
                                            ),
                                          ],
                                        ),

有谁能帮帮我!

【问题讨论】:

    标签: flutter dart speech-to-text


    【解决方案1】:

    这个错误是因为你没有实例化stt.SpeechToText对象然后调用包的初始化函数。

    您可以使用以下方法对其进行实例化

    stt.SpeechToText _speech = stt.SpeechToText();
    

    【讨论】:

    • 'package:flutter/src/animation/animation_controller.dart':断言失败:第 780 行第 12 行:'elapsedInSeconds >= 0.0':不正确。
    • 新错误似乎并非源自SpeechToText 实现。错误可能来自您的 AvatarGlow 小部件,因为这是页面上唯一带有动画的内容。
    • 但是,请尝试运行flutter clean,然后运行flutter run,看看错误是否仍然存在。
    • 现在我的代码是干净的,我没有任何错误,但听的功能不起作用我说测试,这没有显示在我的文本标签中
    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2021-08-08
    • 2020-03-01
    相关资源
    最近更新 更多