Skip to content

hook dispose it feels weird #444

Closed
Closed
@luoyi58624

Description

@luoyi58624

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();
  }
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions