Skip to content

Barometer

Streams barometer readings (atmospheric pressure in hPa). Useful for altitude calculations and weather-related experiences.

Note

Supported platforms: Android, iOS. Barometer APIs are not exposed on the Web or desktop platforms and iOS ignores custom sampling intervals.

Inherits: Service

Properties

Events

Examples#

import flet as ft


def main(page: ft.Page):
    intro = ft.Text("Atmospheric pressure (hPa).")
    reading = ft.Text("Waiting for data...")

    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.session.store.set(
        "barometer_service",
        ft.Barometer(
            on_reading=handle_reading,
            on_error=handle_error,
            interval=ft.Duration(milliseconds=500),
        ),
    )

    page.add(intro, reading)


ft.run(main)

Properties#

cancel_on_error class-attribute instance-attribute #

cancel_on_error: bool = True

Whether the stream subscription should cancel on the first sensor error.

enabled class-attribute instance-attribute #

enabled: bool = True

Whether the sensor should be sampled. Disable to stop streaming.

interval class-attribute instance-attribute #

interval: Duration | None = None

Desired sampling interval provided as a Duration. Defaults to 200 ms, though some platforms (such as iOS) ignore custom sampling intervals.

Events#

on_error class-attribute instance-attribute #

on_error: EventHandler[SensorErrorEvent] | None = None

Fired when the platform reports a sensor error. event.message is the error description.

on_reading class-attribute instance-attribute #

on_reading: EventHandler[BarometerReadingEvent] | None = (
    None
)

Fires when a new reading is available.

event contains pressure (hPa) and timestamp (microseconds since epoch).