diff --git a/packages/gamepads_platform_interface/lib/api/gamepad_controller.dart b/packages/gamepads_platform_interface/lib/api/gamepad_controller.dart index 289e650..d431369 100644 --- a/packages/gamepads_platform_interface/lib/api/gamepad_controller.dart +++ b/packages/gamepads_platform_interface/lib/api/gamepad_controller.dart @@ -1,3 +1,6 @@ +import 'dart:async'; + +import 'package:gamepads_platform_interface/api/gamepad_event.dart'; import 'package:gamepads_platform_interface/api/gamepad_state.dart'; import 'package:gamepads_platform_interface/gamepads_platform_interface.dart'; @@ -14,12 +17,14 @@ class GamepadController { final state = GamepadState(); + StreamSubscription? _subscription; + GamepadController({ required this.id, required this.name, required GamepadsPlatformInterface plugin, }) { - plugin.eventsByGamepad(id).listen(state.update); + _subscription = plugin.eventsByGamepad(id).listen(state.update); } factory GamepadController.parse( @@ -30,4 +35,9 @@ class GamepadController { final name = map['name'] as String; return GamepadController(id: id, name: name, plugin: plugin); } + + Future dispose() async { + await _subscription?.cancel(); + _subscription = null; + } }