flutter-plugins icon indicating copy to clipboard operation
flutter-plugins copied to clipboard

Listener will be binded twice while its parent rebuild

Open BikonLI opened this issue 7 months ago • 0 comments

class FileDropBox extends StatelessWidget {
  FileDropBox({super.key});

  final controller = Get.put(FileDropController());
  @override
  Widget build(BuildContext context) {
    return DropTarget(
      onDragDone: (details) {
        final paths = details.files.map((file) => file.path).toList();
        debugPrint("onDragDone!!!");
        controller.onFilesDropped(paths);
      },
      child: const SizedBox.expand(),
    );
  }
}

For example, when the stateless widget FileDropBox rebuild, DropTarget will add a new onDragDone listener. Which Means when I drop file to this widget, it will run onDragDone twice.

When FileDropBox rebuild again, onDragDone will run for three times.

BikonLI avatar Apr 21 '25 15:04 BikonLI