Skip to content

Commit bc12a8c

Browse files
Audio dispose fix (#5893)
* Add resource cleanup in audio and component classes Dispose of the audio player in AudioService and clear hooks in Component on unmount to ensure proper resource management and prevent potential memory leaks. * Refactor flashlight service to remove custom exceptions Removed custom exception classes and error handling from the flet-flashlight Python and Dart implementations. The Flashlight service methods now directly invoke platform methods without raising or returning custom errors. Updated example usage and cleaned up related imports and documentation. * Rename Flashlight methods to 'on' and 'off' Changed method names from 'turn_on' and 'turn_off' to 'on' and 'off' in the Flashlight class and updated example usage accordingly for consistency and brevity. * Await clipboard setData operation Added 'await' to Clipboard.setData in ClipboardService to ensure the operation completes before proceeding. This improves reliability when setting clipboard data asynchronously. * Replace ElevatedButton with Button in example Updated the flashlight example to use ft.Button instead of ft.ElevatedButton for consistency or UI preference. * Remove Flashlight exception docs and nav entries Deleted documentation files for Flashlight exception classes and removed their references from the mkdocs navigation and the index page. This streamlines the documentation by eliminating detailed exception class pages for Flashlight.
1 parent 0441768 commit bc12a8c

File tree

18 files changed

+21
-237
lines changed

18 files changed

+21
-237
lines changed

packages/flet/lib/src/services/clipboard.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ClipboardService extends FletService {
2222
debugPrint("ClipboardService.$name($args)");
2323
switch (name) {
2424
case "set":
25-
Clipboard.setData(ClipboardData(text: args["data"]));
25+
await Clipboard.setData(ClipboardData(text: args["data"]));
2626
case "get":
2727
var data = await Clipboard.getData(Clipboard.kTextPlain);
2828
return data?.text;

sdk/python/examples/services/audio/declarative_1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def App():
1313

1414
audio, _ = ft.use_state(
1515
lambda: fta.Audio(
16-
src="https://luan.xyz/files/audio/ambient_c_motion.mp3",
16+
src="https://github.com/flet-dev/media/raw/refs/heads/main/sounds/sweet-life-luxury-chill-438146.mp3",
1717
autoplay=False,
1818
on_duration_change=lambda e: set_duration(e.duration.in_milliseconds),
1919
on_position_change=lambda e: set_position(e.position),

sdk/python/examples/services/flashlight/example_1.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44

55
def main(page: ft.Page):
6-
async def toggle_flashlight():
7-
flashlight = ffl.Flashlight()
8-
await flashlight.toggle()
6+
async def turn_on_flashlight():
7+
await ffl.Flashlight().on()
98

10-
page.add(ft.TextButton("toggle", on_click=toggle_flashlight))
9+
async def turn_off_flashlight():
10+
await ffl.Flashlight().off()
11+
12+
page.add(
13+
ft.Button("Turn On Flashlight", on_click=turn_on_flashlight),
14+
ft.Button("Turn Off Flashlight", on_click=turn_off_flashlight),
15+
)
1116

1217

1318
ft.run(main)

sdk/python/packages/flet-audio/src/flutter/flet_audio/lib/src/audio.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class AudioService extends FletService {
178178
_onStateChangedSubscription?.cancel();
179179
_onPositionChangedSubscription?.cancel();
180180
_onSeekCompleteSubscription?.cancel();
181+
player.dispose();
181182
super.dispose();
182183
}
183184
}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
from .exceptions import (
2-
FlashlightDisableException,
3-
FlashlightDisableExistentUserException,
4-
FlashlightDisableNotAvailableException,
5-
FlashlightEnableException,
6-
FlashlightEnableExistentUserException,
7-
FlashlightEnableNotAvailableException,
8-
FlashlightException,
9-
)
101
from .flashlight import Flashlight
112

123
__all__ = [
134
"Flashlight",
14-
"FlashlightDisableException",
15-
"FlashlightDisableExistentUserException",
16-
"FlashlightDisableNotAvailableException",
17-
"FlashlightEnableException",
18-
"FlashlightEnableExistentUserException",
19-
"FlashlightEnableNotAvailableException",
20-
"FlashlightException",
215
]

sdk/python/packages/flet-flashlight/src/flet_flashlight/exceptions.py

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
from typing import Optional
2-
31
import flet as ft
42

5-
from .exceptions import (
6-
FlashlightDisableException,
7-
FlashlightDisableExistentUserException,
8-
FlashlightDisableNotAvailableException,
9-
FlashlightEnableException,
10-
FlashlightEnableExistentUserException,
11-
FlashlightEnableNotAvailableException,
12-
FlashlightException,
13-
)
14-
153
__all__ = ["Flashlight"]
164

175

@@ -21,69 +9,20 @@ class Flashlight(ft.Service):
219
A control to use FlashLight. Works on iOS and Android.
2210
"""
2311

24-
on = False
25-
"""
26-
Whether the flashlight is currently turned on.
27-
"""
28-
29-
on_error: Optional[ft.ControlEventHandler["Flashlight"]] = None
30-
"""
31-
Fires when an error occurs.
32-
33-
The [`data`][flet.Event.data] property of the event handler argument
34-
contains information on the error.
35-
"""
36-
37-
async def turn_on(self):
12+
async def on(self):
3813
"""
3914
Turns the flashlight on.
4015
"""
41-
r = await self._invoke_method("on")
42-
if r is True:
43-
self.on = True
44-
else: # error occured
45-
error_type = r.get("error_type")
46-
error_msg = r.get("error_msg")
47-
if error_type == "EnableTorchExistentUserException":
48-
raise FlashlightEnableExistentUserException(error_msg)
49-
elif error_type == "EnableTorchNotAvailableException":
50-
raise FlashlightEnableNotAvailableException(error_msg)
51-
else:
52-
raise FlashlightEnableException(error_msg)
16+
await self._invoke_method("on")
5317

54-
async def turn_off(self):
18+
async def off(self):
5519
"""
5620
Turns the flashlight off.
5721
"""
58-
r = await self._invoke_method("off")
59-
if r is True:
60-
self.on = False
61-
else: # error occured
62-
error_type = r.get("error_type")
63-
error_msg = r.get("error_msg")
64-
if error_type == "DisableTorchExistentUserException":
65-
raise FlashlightDisableExistentUserException(error_msg)
66-
elif error_type == "DisableTorchNotAvailableException":
67-
raise FlashlightDisableNotAvailableException(error_msg)
68-
else:
69-
raise FlashlightDisableException(error_msg)
70-
71-
async def toggle(self):
72-
"""
73-
Toggles the flashlight on and off.
74-
"""
75-
if self.on:
76-
await self.turn_off()
77-
else:
78-
await self.turn_on()
22+
await self._invoke_method("off")
7923

8024
async def is_available(self):
8125
"""
8226
Checks if the flashlight is available on the device.
8327
"""
84-
r = await self._invoke_method("is_available")
85-
if isinstance(r, bool):
86-
return r
87-
else: # error occured
88-
error_msg = r.get("error_msg")
89-
raise FlashlightException(error_msg)
28+
return await self._invoke_method("is_available")

sdk/python/packages/flet-flashlight/src/flutter/flet_flashlight/lib/src/flashlight.dart

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,13 @@ class FlashlightControl extends FletService {
1515
Future<dynamic> _invokeMethod(String name, dynamic args) async {
1616
debugPrint("Flashlight.$name($args)");
1717
if (isMobilePlatform()) {
18-
Map<String, String?>? errorInfo;
1918
switch (name) {
2019
case "on":
21-
try {
22-
await TorchLight.enableTorch();
23-
return true;
24-
} catch (e) {
25-
if (e is EnableTorchExistentUserException) {
26-
errorInfo = {
27-
"error_type": "EnableTorchExistentUserException",
28-
"error_msg": e.message
29-
};
30-
} else if (e is EnableTorchNotAvailableException) {
31-
errorInfo = {
32-
"error_type": "EnableTorchNotAvailableException",
33-
"error_msg": e.message
34-
};
35-
} else {
36-
errorInfo = {
37-
"error_type": "EnableTorchException",
38-
"error_msg": (e as EnableTorchException).message
39-
};
40-
}
41-
control.triggerEvent("error", errorInfo);
42-
debugPrint(
43-
"Error enabling Flashlight: ${errorInfo["error_type"]}(${errorInfo["error_msg"]})");
44-
return errorInfo;
45-
}
20+
await TorchLight.enableTorch();
4621
case "off":
47-
try {
48-
await TorchLight.disableTorch();
49-
return true;
50-
} catch (e) {
51-
if (e is DisableTorchExistentUserException) {
52-
errorInfo = {
53-
"error_type": "DisableTorchExistentUserException",
54-
"error_msg": e.message
55-
};
56-
} else if (e is DisableTorchNotAvailableException) {
57-
errorInfo = {
58-
"error_type": "DisableTorchNotAvailableException",
59-
"error_msg": e.message
60-
};
61-
} else {
62-
errorInfo = {
63-
"error_type": "DisableTorchException",
64-
"error_msg": (e as DisableTorchException).message
65-
};
66-
}
67-
control.triggerEvent("error", errorInfo);
68-
debugPrint(
69-
"Error disabling Flashlight: ${errorInfo["error_type"]}(${errorInfo["error_msg"]})");
70-
return errorInfo;
71-
}
22+
await TorchLight.disableTorch();
7223
case "is_available":
73-
try {
74-
final available = await TorchLight.isTorchAvailable();
75-
return available;
76-
} on EnableTorchException catch (e) {
77-
errorInfo = {
78-
"error_type": "EnableTorchException",
79-
"error_msg": e.message
80-
};
81-
control.triggerEvent("error", errorInfo);
82-
debugPrint(
83-
"Error checking Flashlight availability: EnableTorchException(${e.message})");
84-
return errorInfo;
85-
}
24+
return await TorchLight.isTorchAvailable();
8625
default:
8726
throw Exception("Unknown Flashlight method: $name");
8827
}

sdk/python/packages/flet/docs/flashlight/exceptions/flashlight_disable_exception.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

sdk/python/packages/flet/docs/flashlight/exceptions/flashlight_disable_existent_user_exception.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)