> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/ai/sessions/dart-flutter.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Dart / Flutter

Continue a multi-turn AI conversation by passing **`sessionId`** from the first response into follow-up `sendAI` calls.

## Complete multi-turn example

```dart
import 'package:dnotifier/dnotifier.dart';

const userId = 'user-123';

Future<void> main() async {
  final notifier = DNotifier(
    appId: 'your-app-id',
    secret: 'your-app-secret',
    transport: 'http',
    userId: userId,
    onConnected: () {},
    onMessage: (_) {},
  );

  await notifier.connect();

  // Turn 1 — new session
  final first = await notifier.sendAI(
    senderId: userId,
    message: {'text': 'I need help with billing for order #4821.'},
  );

  final sessionId = _sessionIdFromResponse(first);
  print('Session: $sessionId');

  // Turn 2 — same session
  final second = await notifier.sendAI(
    senderId: userId,
    sessionId: sessionId,
    message: {'text': 'What documents do I need to submit?'},
  );

  print('Follow-up: $second');
}

String? _sessionIdFromResponse(dynamic response) {
  if (response is! Map) return null;
  final metadata = response['metadata'];
  if (metadata is Map) {
    final packet = metadata['packet'];
    if (packet is Map && packet['id'] != null) {
      return packet['id'].toString();
    }
    if (metadata['id'] != null) return metadata['id'].toString();
  }
  return null;
}
```

## Persist session per ticket

```dart
class TicketStore {
  String? sessionIdForTicket(String ticketId) => _sessions[ticketId];
  void saveSession(String ticketId, String sessionId) {
    _sessions[ticketId] = sessionId;
  }

  final Map<String, String> _sessions = {};
}

Future<void> handleUserMessage(
  DNotifier notifier,
  TicketStore store,
  String ticketId,
  String text,
) async {
  var sessionId = store.sessionIdForTicket(ticketId);

  final response = await notifier.sendAI(
    senderId: userId,
    sessionId: sessionId,
    message: {'text': text},
  );

  sessionId ??= _sessionIdFromResponse(response);
  if (sessionId != null) {
    store.saveSession(ticketId, sessionId);
  }
}
```

## Chat-style in a session

```dart
final first = await notifier.sendAI(
  senderId: userId,
  message: {
    'messages': [
      {'role': 'system', 'content': 'You are a billing specialist.'},
      {'role': 'user', 'content': 'I was charged twice.'},
    ],
  },
);

final sessionId = _sessionIdFromResponse(first);

await notifier.sendAI(
  senderId: userId,
  sessionId: sessionId,
  message: {
    'messages': [
      {'role': 'user', 'content': 'Here is my receipt number: R-9912.'},
    ],
  },
);
```

## Learn more

* [**AI history**](https://dnotifier.gitbook.io/product-docs/ai/ai-history/dart-flutter.md)
* [**Session logging**](https://dnotifier.gitbook.io/product-docs/ai/session-logging/dart-flutter.md)
