Product update · · 3 min read

Flutter Data API now works on Android and iOS

Read, send, and subscribe to chat data from one Dart codebase on both mobile platforms.

TalkJS Flutter Data API for Android and iOS

A Flutter app with its own chat screens needs chat data on every platform it ships to. The first release of the TalkJS Flutter Data API ran on Android only, so it couldn't serve the iOS build of the same app.

The TalkJS Flutter Data API now supports both Android and iOS, starting with version 0.2.0 of the talkjs_core_flutter package. From one Dart codebase, you can connect as a user, then read, send, and subscribe to conversations and messages in real time, and show them in your own widgets.

Subscribe to messages with the Flutter Data API

With the Flutter Data API, the same Dart code handles chat data on Android and iOS. Inside an async function, the following example connects as the user alice, prints new messages in a conversation, and sends a reply:

import 'package:talkjs_core_flutter/talkjs_core_flutter.dart';

final session = await getTalkSession(appId: '<APP_ID>', userId: 'alice');
final conversation = await session.conversation('support-alice');

final subscription = conversation.subscribeMessages((messages, loadedAll) {
  for (final message in messages ?? <MessageSnapshot>[]) {
    print('${message.sender?.name ?? "System"}: ${message.plaintext}');
  }
});

await conversation.send('Hi, I have a question about my order.');

Replace <APP_ID> with your own TalkJS app ID, which you can find on the Settings page of your TalkJS dashboard.

subscribeMessages calls your function with the 10 most recent messages, then again whenever a message arrives or someone edits one. Call loadMore on the subscription to load older messages, and unsubscribe when your widget goes away. The list is null if alice isn't a participant in the conversation.

In production, pass a token to getTalkSession to authenticate the user.

What you can build with the Flutter Data API

The Flutter Data API covers more of a chat app than sending and receiving messages:

Upgrade the Flutter Data API from version 0.1.0

Version 0.2.0 of the Flutter Data API raises its minimum versions, which affects anyone upgrading from 0.1.0. Your project now needs Dart 3.12.0 or later (previously 3.11.0) and Flutter 3.44.0 or later (previously 3.3.0). iOS apps need iOS 15 or later, and Android apps still need Android API 24 or later.

Your existing Dart calls keep working after you upgrade, with one exception: forceCreateNew on getTalkSession now defaults to false and no longer accepts null.

The iOS build doesn't support the x86_64 target, which can make flutter build ios fail with an error about uploadFile. The docs have the fix for this iOS build failure.

Choose between the Flutter Data API and the Flutter SDK

TalkJS has two Flutter packages for Android and iOS apps. The Flutter Data API, talkjs_core_flutter, is headless: it gives you chat data and real-time updates, and you build the screens yourself. The Flutter SDK, talkjs_flutter, is a separate package with pre-built ChatBox and ConversationList widgets, so use it if you want a ready-made chat screen.

If you write separate native apps instead of one Flutter app, use the Swift Data API on iOS and the Kotlin Data API on Android.

The Flutter Data API connects as one user, so it's meant for client apps. For server-side access to all users and conversations, use the REST API.

Get started with the Flutter Data API on iOS

Run flutter pub add talkjs_core_flutter to add version 0.2.0 or later from pub.dev. The Flutter Data API docs cover setup and the full reference.