'Flutter Text To Speech conversation length

I want to tell the user the name of the object detected with the Tflite model. I added some code for this. The problem is that since I wrote the speak method in return, speak is called again on every detected object, and the detected object switches to the new word halfway through before it is fully spoken. How can I overcome this problem.

bool _finded = false;
late final List<dynamic> _res;

class _BndBoxState extends State<BndBox> {
  bool _finded = false;
  late final List<dynamic> _res;
  final TextToSpeech _textToSpeech = TextToSpeech();

  @override
  Widget build(BuildContext context) {
    List<Widget> _renderBoxes() {
      widget.results.map((e) {
        _textToSpeech.speak("${e['detectedClass']}");
      });

      return widget.results.map((re) {
        var _x = re["rect"]["x"];
        var _w = re["rect"]["w"];
        var _y = re["rect"]["y"];
        var _h = re["rect"]["h"];
        var scaleW, scaleH, x, y, w, h;

        if (widget.search == "${re['detectedClass']}") {
          _res.add(re['detectedClass']);
          setState(() {
            _finded = true;
          });
          if (_finded == true) {
            widget._textToSpeech.speak("${re['detectedClass'] + 'finded'}");
          }
        }

        if (widget.screenH / widget.screenW >
            widget.previewH / widget.previewW) {
          scaleW = widget.screenH / widget.previewH * widget.previewW;
          scaleH = widget.screenH;
          var difW = (scaleW - widget.screenW) / scaleW;
          x = (_x - difW / 2) * scaleW;
          w = _w * scaleW;
          if (_x < difW / 2) w -= (difW / 2 - _x) * scaleW;
          y = _y * scaleH;
          h = _h * scaleH;
        } else {
          scaleH = widget.screenW / widget.previewW * widget.previewH;
          scaleW = widget.screenW;
          var difH = (scaleH - widget.screenH) / scaleH;
          x = _x * scaleW;
          w = _w * scaleW;
          y = (_y - difH / 2) * scaleH;
          h = _h * scaleH;
          if (_y < difH / 2) h -= (difH / 2 - _y) * scaleH;
        }

        return Positioned(
          left: math.max(0, x),
          top: math.max(0, y),
          width: w,
          height: h,
          child: Container(
            padding: const EdgeInsets.only(top: 5.0, left: 5.0),
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.amber,
                width: 1.0,
              ),
            ),
            child: Text(
              "${re["detectedClass"]} ${(re["confidenceInClass"] * 100).toStringAsFixed(0)}%",
              style: const TextStyle(
                color: Colors.amber,
                fontSize: 14.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        );
      }).toList();
    }

    return Stack(
      children: _renderBoxes(),
    );
  }




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source