> 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/getting-started/first-connection/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

This guide connects a DNotifier client over **WebSocket** from Dart or Flutter. You need [**credentials**](https://dnotifier.gitbook.io/product-docs/getting-started/credentials.md) and the [**Dart SDK installed**](https://dnotifier.gitbook.io/product-docs/getting-started/installation/dart-flutter.md).

## Complete example

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

Future<void> main() async {
  final notifier = DNotifier(
    appId: 'your-app-id',
    secret: 'your-app-secret',
    transport: 'ws',
    userId: 'user-alice',
    onConnected: () {
      print('Connected — ready to send messages');
      final limits = notifier.getPlanLimits();
      print('Plan limits: $limits');
    },
    onMessage: (DNotifierMessage msg) {
      print('Incoming from: ${msg.metadata.sender}');
      print('Payload: ${msg.payload.toJSON()}');
    },
    onDisconnected: ({code, reason}) {
      print('Disconnected: code=$code reason=$reason');
    },
  );

  try {
    await notifier.connect();
    print('isConnected: ${notifier.isConnected}');
    print('messageSizeLimit: ${notifier.messageSizeLimit}');
  } catch (e) {
    print('Connect failed: $e');
  }
}
```

Replace `'your-app-id'` and `'your-app-secret'` with values from the dashboard, or load them from environment defines — see [**Credentials & environment**](https://dnotifier.gitbook.io/product-docs/getting-started/credentials.md).

Run in a pure Dart project:

```bash
dart run bin/connect.dart
```

Run in Flutter:

```bash
flutter run
```

Expected console output:

```
Connected — ready to send messages
Plan limits: Instance of 'DNotifierPlanLimits'
isConnected: true
messageSizeLimit: 32768
```

## Flutter widget example

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

class DNotifierScreen extends StatefulWidget {
  const DNotifierScreen({super.key});

  @override
  State<DNotifierScreen> createState() => _DNotifierScreenState();
}

class _DNotifierScreenState extends State<DNotifierScreen> {
  late final DNotifier _notifier;
  String _status = 'Connecting…';

  @override
  void initState() {
    super.initState();
    _notifier = DNotifier(
      appId: const String.fromEnvironment('DNOTIFIER_APP_ID'),
      secret: const String.fromEnvironment('DNOTIFIER_SECRET'),
      transport: 'ws',
      userId: 'user-alice',
      onConnected: () => setState(() => _status = 'Connected'),
      onMessage: (msg) {
        debugPrint('Message: ${msg.payload.toJSON()}');
      },
      onDisconnected: ({code, reason}) {
        setState(() => _status = 'Disconnected');
      },
    );
    _notifier.connect().catchError((e) {
      setState(() => _status = 'Error: $e');
    });
  }

  @override
  void dispose() {
    _notifier.disconnect();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('DNotifier')),
      body: Center(child: Text(_status)),
    );
  }
}
```

## HTTP transport example

For AI/RAG/workflows without a persistent socket:

```dart
final notifier = DNotifier(
  appId: 'your-app-id',
  secret: 'your-app-secret',
  transport: 'http',
  userId: 'user-alice',
  onConnected: () => print('Authenticated via HTTP'),
);

await notifier.connect();
// Ready for sendAI(), search(), runWorkflow(), etc.
```

## Common errors

| Error                                                   | Likely cause                                |
| ------------------------------------------------------- | ------------------------------------------- |
| `ArgumentError: appId, secret, and userId are required` | Empty constructor argument                  |
| `Exception: Authentication failed`                      | Wrong secret or appId                       |
| `StateError: Not connected`                             | Called `send()` before `connect()` finished |
| Socket errors on device                                 | Network or certificate issues               |

## What happens under the hood

1. SDK sends credentials to the DNotifier auth endpoint
2. Auth returns `token` and plan `limits`
3. WebSocket connects to the DNotifier realtime endpoint
4. Client sends handshake with token
5. Server acknowledges → `onConnected` callback runs

## Next step

Send a message to another user:

→ [**Your first message — Dart / Flutter**](https://dnotifier.gitbook.io/product-docs/getting-started/first-message/dart-flutter.md)
