Mix-in: Getting ``dispose() implementations must always call their superclass dispose() method'' although super.dispose() is called

1,200 views
Skip to first unread message

Hugo van Galen

unread,
Aug 21, 2017, 3:59:13 AM8/21/17
to Flutter Dev
Hi all,

I am using the [ChangeNotifier] class as a mix-in and receiving this exception during the dispose() phase:

``_TestWidgetState.dispose failed to call super.dispose.
dispose() implementations must always call their superclass dispose() method, to ensure that all the
resources used by the widget are fully released.''

Because my code does call super.dispose(), I do not fully grasp why this is happening, although I did discover that, if the 'with ChangeNotifier' in my class definition is removed, I no longer receive this exception. (But then my class is obviously no longer listenable.)

The [ChangeNotifier] docs state that it can be used as a mix-in. However, after glancing through the Flutter code-base, for usages of the ChangeNotifier, I can see that it is only ever extended rather than mixed-in. So perhaps, this is simply a case of documentation being wrong?


abstract class _TestState<T extends StatefulWidget>
extends State<T>
with ChangeNotifier {
@override
Widget build(BuildContext context);
}

class _TestWidgetState extends _TestState<TestWidget> {
@override
Widget build(BuildContext context) => const Text( "foo" );
}

class TestWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _TestWidgetState();
}

Ian Hickson

unread,
Aug 21, 2017, 12:22:05 PM8/21/17
to Hugo van Galen, Flutter Dev
You can only mix in ChangeNotifier if your base class doesn't define a "dispose" method that @mustCallSuper.

There's two simple ways to deal with this, you can either just have a ChangeNotifier tear-of (i.e. rather than implementing it directly on the State, create a second object whose lifetime you manage that you then hand to people who need it), or you can implement the Listenable API directly rather than using the code from ChangeNotifier.

Please don't hesitate to file a bug if you think the ChangeNotifier docs should be improved.


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

--
Ian Hickson

😸
Reply all
Reply to author
Forward
0 new messages