> 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/reference/configuration/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

Package: `dnotifier` ([pub.dev](https://pub.dev/packages/dnotifier))

## Constructor

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

final notifier = DNotifier(
  appId: 'your-app-id',
  secret: 'your-app-secret',
  transport: 'ws', // or 'http'
  userId: 'user-123',
  logs: false,
  // url: 'wss://YOUR_HOST', // optional — only if provided by DNotifier
  onConnected: () {},
  onMessage: (msg) {},
  onDisconnected: ({code, reason}) {},
  connectWebSocket: null, // optional custom WebSocket factory
);
```

## Option reference

| Parameter               | Type                                          | Required | Default                      | Description                                            |
| ----------------------- | --------------------------------------------- | -------- | ---------------------------- | ------------------------------------------------------ |
| `appId`                 | `String`                                      | ✅        | —                            | DNotifier application id                               |
| `secret`                | `String`                                      | ✅        | —                            | Application secret                                     |
| `transport`             | `String`                                      | ✅        | —                            | `"ws"` or `"http"`                                     |
| `userId`                | `String`                                      | ✅        | —                            | User or agent id                                       |
| `logs`                  | `bool`                                        | —        | `false`                      | Enable AI session logging                              |
| `url`                   | `String`                                      | —        | production WSS (SDK default) | Custom WebSocket URL only if provided by DNotifier     |
| `onConnected`           | `void Function()?`                            | —        | `null`                       | Ready callback                                         |
| `onMessage`             | `void Function(DNotifierMessage)?`            | —        | `null`                       | Incoming message callback                              |
| `onDisconnected`        | `void Function({int? code, String? reason})?` | —        | `null`                       | Disconnect callback                                    |
| `connectWebSocket`      | `ConnectWebSocket?`                           | —        | `null`                       | Injectable WebSocket connector for tests               |
| `permissionEnforcement` | `String`                                      | —        | `'backend'`                  | `'backend'` or `'disabled'` for tool permission checks |
| `onAgentEvent`          | `void Function(Map<String, dynamic>)?`        | —        | `null`                       | Local agent/tool/MCP event callback                    |

Throws `ArgumentError` if `appId`, `secret`, or `userId` is empty, or if `transport` is not `"ws"` or `"http"`.

Agent tools and MCP: see [**Tools & MCP agents**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/dart-flutter.md).

## Flutter example

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

final notifier = DNotifier(
  appId: const String.fromEnvironment('DNOTIFIER_APP_ID'),
  secret: const String.fromEnvironment('DNOTIFIER_SECRET'),
  transport: 'ws',
  userId: 'user-alice',
  onConnected: () => debugPrint('Connected'),
  onMessage: (msg) => debugPrint(msg.payload.toJSON()),
  onDisconnected: ({code, reason}) => debugPrint('Disconnected: $reason'),
);

await notifier.connect();
```

## HTTP transport example

```dart
final notifier = DNotifier(
  appId: appId,
  secret: secret,
  transport: 'http',
  userId: 'service-bot',
  logs: true,
  onConnected: () {},
);

await notifier.connect();
```

## Instance fields (after `connect()`)

| Field              | Type      | Description                           |
| ------------------ | --------- | ------------------------------------- |
| `appId`            | `String`  | Constructor value                     |
| `secret`           | `String`  | Constructor value                     |
| `userId`           | `String`  | Constructor value                     |
| `transport`        | `String`  | `"ws"` or `"http"`                    |
| `url`              | `String`  | WebSocket URL                         |
| `logs`             | `bool`    | Logging flag                          |
| `isConnected`      | `bool`    | Connection state                      |
| `aiEnabled`        | `bool`    | Plan AI flag                          |
| `authToken`        | `String?` | Auth token (nullable until connected) |
| `messageSizeLimit` | `int`     | Chunk size limit in bytes             |

## `disconnect()`

Dart exposes an explicit close method:

```dart
notifier.disconnect(); // closes WebSocket, sets isConnected = false
```

## Custom WebSocket URL

```dart
final notifier = DNotifier(
  appId: appId,
  secret: secret,
  transport: 'ws',
  userId: userId,
  url: 'wss://YOUR_HOST', // only if provided by DNotifier
);
```

→ [Endpoints & custom URL](https://dnotifier.gitbook.io/product-docs/reference/endpoints.md)

## Callback typedefs

```dart
typedef OnConnected = void Function();
typedef OnMessageCallback = void Function(DNotifierMessage message);
typedef OnDisconnectedCallback = void Function({int? code, String? reason});
```
