Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StatefulElements are considered "not disposed" if an exception happens in a State lifecycle #221

Closed
rrousselGit opened this issue Feb 28, 2024 · 2 comments · Fixed by #238

Comments

@rrousselGit
Copy link

Hello!

Consider the following test:

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Example', (tester) async {
    await tester.pumpWidget(const MyWidget());

    expect(tester.takeException(), isUnimplementedError);
  });
}

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  void initState() {
    super.initState();
    throw UnimplementedError();
  }

  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

This will fail with:

  Expected: leak free
    Actual: <Instance of 'Leaks'>
     Which: contains leaks:
            # The text is generated by leak_tracker.
            # For leak troubleshooting tips open:
            # https://github.com/dart-lang/leak_tracker/blob/main/doc/TROUBLESHOOT.md
            notDisposed:
              total: 1
              objects:
                StatefulElement:
                  test: Example
                  identityHashCode: 470187304       

I assume the issue is that if an exception happens in initState, State.dispose never gets called.

@polina-c
Copy link
Contributor

Sorry for delay on it.
Yes, tests with exceptions do not dispose state.
Such tests should be opted out from leak tracking:

testWidgets('async onInit throws FlutterError',
  experimentalLeakTesting: LeakTesting.settings.withIgnoredAll(), // leaking by design because of exception
  (WidgetTester tester) async {
...  

@polina-c
Copy link
Contributor

Documented this: #238
Thanks for flagging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants