Skip to main content

thingsboard_client 5.0.0 copy "thingsboard_client: ^5.0.0" to clipboard
thingsboard_client: ^5.0.0 copied to clipboard

ThingsBoard REST API client for Flutter. JWT auth, auto-refresh, WebSocket telemetry, typed error handling.

example/main.dart

// ignore_for_file: avoid_print
import 'package:thingsboard_client/thingsboard_client.dart';

const thingsBoardApiEndpoint = 'http://localhost:8080';

void main() async {
  // 1. Create client
  final tbClient = ThingsboardClient(thingsBoardApiEndpoint);

  try {
    // 2. Login with username and password
    await tbClient.login(
      LoginRequest(
        (b) => b
          ..username = 'tenant@thingsboard.org'
          ..password = 'tenant',
      ),
    );
    print('Logged in: ${tbClient.isAuthenticated()}');

    // 3. Paginated device list
    final deviceApi = tbClient.getDeviceControllerApi();
    final response = await deviceApi.getTenantDevices(pageSize: 10, page: 0);
    final pageData = response.data;
    if (pageData != null) {
      print('Total devices: ${pageData.totalElements}');
      for (final device in pageData.data ?? <Device>[]) {
        print('  - ${device.name} (${device.type})');
      }
    }

    // 4. Latest telemetry of the first device over the WebSocket.
    final devices = pageData?.data ?? <Device>[];
    if (devices.isNotEmpty) {
      final subscriber = TelemetrySubscriber.createEntityAttributesSubscription(
        telemetryService: tbClient.getTelemetryService(),
        entityId: devices.first.id!,
        attributeScope: LatestTelemetry.LATEST_TELEMETRY.toShortString(),
      );
      final update = subscriber.attributeDataStream.first;
      subscriber.subscribe();
      final data = await update.timeout(const Duration(seconds: 10));
      print(
        'Latest telemetry of ${devices.first.name}: '
        '${data.map((e) => '${e.key}=${e.value}').join(', ')}',
      );
      subscriber.unsubscribe();
    }

    // 5. Logout
    await tbClient.logout();
    print('Logged out');
  } catch (e) {
    if (e is ThingsboardError) {
      print('ThingsBoard error: ${e.message} (${e.errorCode})');
    } else {
      print('Error: $e');
    }
  }
}
16
likes
150
points
963
downloads

Documentation

API reference

Publisher

verified publisherthingsboard.io

Weekly Downloads

ThingsBoard REST API client for Flutter. JWT auth, auto-refresh, WebSocket telemetry, typed error handling.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#thingsboard #iot #rest-api #flutter #websocket

License

BSD-3-Clause (license)

Dependencies

built_collection, built_value, collection, dio, jwt_decoder, one_of, one_of_serializer, pretty_dio_logger, web_socket_channel

More

Packages that depend on thingsboard_client