Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Do I need to call GestureRecognizer.dispose in StatelessWidget (TextSpan/recognizer)?

Tags:

flutter

I am reading this doc page: https://docs.flutter.io/flutter/painting/TextSpan/recognizer.html.

The example included in this page is a StatefulWidget and the doc says The code that owns the GestureRecognizer object must call GestureRecognizer.dispose when the TextSpan object is no longer used..

I am wondering if I can use the recognizer of TextSpan in a StatelessWidget?

If so, do I need to call dispose somewhere? I have no idea where to call it.

like image 445
sgon00 Avatar asked Nov 01 '25 04:11

sgon00


1 Answers

You cannot do that in a StatelessWidget. You will have to convert it into a StatefulWidget and override the dispose method of State:

class Foo extends StatefulWidget {
  @override
  _FooState createState() => _FooState();
}

class _FooState extends State<Foo> {
  GestureRecognizer gestureRecognizer;

  @override
  void dispose() {
    gestureRecognizer?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
like image 101
Rémi Rousselet Avatar answered Nov 02 '25 18:11

Rémi Rousselet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!