Barometer
**Overview**
**Platform Support**
**iOS Configuration Requirement**
NSMotionUsageDescription to your pyproject.toml file. [tool.flet.ios.info]
NSMotionUsageDescription = "This app requires access to the barometer to provide altitude information."
**Inheritance**
flet.Service.**Properties**
cancel_on_error: boolTrue
- Description: Determines if the stream subscription should be cancelled upon encountering the first sensor error.
enabled: boolTrue
- Description: Controls whether the sensor should be actively sampled. Setting to False stops the data stream.
interval: Duration | NoneNone (platform-dependent, typically 200ms. iOS ignores custom intervals.)
- Description: Specifies the desired sampling interval using a flet.Duration object.**Events**
on_error: EventHandler[SensorErrorEvent] | Noneevent.message attribute contains the error description.
on_reading: EventHandler[BarometerReadingEvent] | Noneevent object contains:
- pressure: float (in hPa)
- timestamp: int (microseconds since epoch)**Example Usage**
import flet as ftdef main(page: ft.Page):
def handle_reading(e: ft.BarometerReadingEvent):
reading.value = f"{e.pressure:.2f} hPa"
page.update()
def handle_error(e: ft.SensorErrorEvent):
page.add(ft.Text(f"Barometer error: {e.message}"))
page.services.append(
ft.Barometer(
on_reading=handle_reading,
on_error=handle_error,
interval=ft.Duration(milliseconds=500),
)
)
page.add(
ft.Text("Atmospheric pressure (hPa)."),
reading := ft.Text("Waiting for data..."),
)
ft.run(main)
Previous
arrow_back
Accelerometer