Flutter:语音转文本错误方法 'initialize' 被调用为 null

Flutter : Speech to text error The method 'initialize' was called on null

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

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'initialize' was called on null. E/flutter (13534): Receiver: null E/flutter (13534): Tried calling: initialize(onError: Closure: (SpeechRecognitionError) => void, onStatus: Closure: (String) => void)

请注意,我将录制权限放在 android 清单中

这是我用于语音转文本的功能

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,
                                              ),
                                            ),
                                          ],
                                        ),

所以有人可以帮助我吗!

此错误是由于您没有实例化 stt.SpeechToText 对象然后调用包的初始化函数。

您可以使用以下实例化它

stt.SpeechToText _speech = stt.SpeechToText();