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

hook dispose it feels weird #444

Closed
luoyi58624 opened this issue Aug 30, 2024 · 1 comment
Closed

hook dispose it feels weird #444

luoyi58624 opened this issue Aug 30, 2024 · 1 comment
Assignees
Labels
bug Something isn't working needs triage

Comments

@luoyi58624
Copy link

luoyi58624 commented Aug 30, 2024

Describe the bug
When I comment out the first hook, dispose prints exactly the last hook,
Is the logic correct here, or is there something wrong with the way I defined hook

To Reproduce

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends HookWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    // final one = useDemo(1);
    final two = useDemo(2);
    final three = useDemo(3);
    return const Scaffold(
      body: Center(
        child: Text('Hello World!'),
      ),
    );
  }
}

T useDemo<T>(T value) {
  return use(_DemoHook(value));
}

class _DemoHook<T> extends Hook<T> {
  const _DemoHook(this.value);

  final T value;

  @override
  _DemoHookState<T> createState() => _DemoHookState();
}

class _DemoHookState<T> extends HookState<T, _DemoHook<T>> {
  late T value = hook.value;

  @override
  void initHook() {
    super.initHook();
    print('initHook');
    print('$value');
  }

  @override
  T build(BuildContext context) => value;

  @override
  void dispose() {
    print('dispose');
    print('$value');
    super.dispose();
  }
}
@luoyi58624 luoyi58624 added bug Something isn't working needs triage labels Aug 30, 2024
@rrousselGit
Copy link
Owner

I see all hooks disposed, in reverse order:

dispose
3
dispose
2

This is normal. So I'm not sure what the issue is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants