website/blog/2025-12-10-sensor-and-system-services.md
We've just merged a pull request introducing 10 new device and platform services to the Flet SDK, significantly expanding access to hardware sensors and system capabilities.
Sensor services mostly work on iOS and Android devices. Using them in a web app is also possible to some extent, but only under specific browser and hardware conditions.
System services are supported on all mobile and desktop platforms.
Install the latest Flet 0.70.0.devXYZ pre-release.
Use the flet debug command to run your app on a real iOS/Android device or emulator.
Testing in the Android emulator is especially convenient, as it allows you to experiment with virtually all available sensors:
Previously, some services were exposed as page properties, such as page.clipboard or page.shared_preferences.
All services are now standalone and should be used by creating their own instances. For example:
clipboard = ft.Clipboard()
await clipboard.set("Hello, world!")
or simply:
await ft.Clipboard().set("Hello, world!")
When you create a new instance of a service, it is automatically registered within the page and disposed of when it is no longer referenced - for example, at the end of a method call.
However, if you need to assign an event handler to a service that must persist across method calls, you must keep a reference to that service to prevent it from being disposed.
In an imperative Flet app, you can use the page.services list, which holds service instances. For example:
async def main(page: ft.Page):
battery = ft.Battery()
page.services.append(battery) # need to keep a reference to the service
async def on_state_change(e: ft.BatteryStateChangeEvent):
print(f"State changed: {e.state}")
battery.on_state_change = on_state_change
...
In a declarative Flet app, you can use the use_ref or use_state hook to hold a service reference. For example:
import flet as ft
@ft.component
def App():
async def on_state_change(e: ft.BatteryStateChangeEvent):
print(f"State changed: {e.state}")
ft.use_ref(lambda: ft.Battery(on_state_change=on_state_change))
return ft.Text("Battery status app")
ft.run(lambda page: page.render(App))
That's all for today, folks! Happy Fletting!