Search code examples
flutterdartspeech-to-text

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


I'm trying to add speech to text method to my application to convert my but i have this error

[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)

note that i'm putting the permission of recording in android manifest

and this is my function which i use for speech to text

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();
      }
    }
  }

and this is my code for displaying it

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

so can any one help me please !


Solution

  • This error occurred due to you not instantiating the stt.SpeechToText object and then calling the package's initialize function.

    You can instantiate it using the following

    stt.SpeechToText _speech = stt.SpeechToText();