> 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/send-prompt/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

Send a **single AI prompt** over HTTP and await the response from `sendAI`.

## Complete 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: () => print('Authenticated'),
    onMessage: (_) {},
    onDisconnected: ({code, reason}) {},
  );

  await notifier.connect();

  if (!notifier.aiEnabled) {
    throw StateError('AI is not enabled for this plan');
  }

  final response = await notifier.sendAI(
    senderId: userId,
    message: {
      'text': 'Summarize our refund policy in two sentences.',
    },
  );

  final answer = _extractContent(response);
  print('AI: $answer');
}

String _extractContent(dynamic response) {
  if (response is Map) {
    final data = response['data'];
    if (data is Map && data['content'] != null) {
      return data['content'].toString();
    }
    if (response['content'] != null) {
      return response['content'].toString();
    }
  }
  return response.toString();
}
```

## Flutter service class

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

class AiService {
  AiService(this._notifier);
  final DNotifier _notifier;

  Future<String> ask(String userId, String question) async {
    final response = await _notifier.sendAI(
      senderId: userId,
      message: {'text': question},
      saveHistory: true,
    );
    return _extractContent(response);
  }
}
```

## Without saving history

```dart
await notifier.sendAI(
  senderId: userId,
  message: {'text': 'Classify this ticket as billing or technical.'},
  saveHistory: false,
);
```

## Environment variables (CLI)

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

Future<void> main() async {
  final notifier = DNotifier(
    appId: Platform.environment['DNOTIFIER_APP_ID']!,
    secret: Platform.environment['DNOTIFIER_SECRET']!,
    transport: 'http',
    userId: 'user-123',
    onConnected: () {},
    onMessage: (_) {},
  );
  await notifier.connect();
  // ...
}
```

## Troubleshooting

| Issue                              | Fix                                        |
| ---------------------------------- | ------------------------------------------ |
| `StateError: Not connected`        | `await notifier.connect()` first           |
| `ArgumentError: senderId required` | Pass non-empty `senderId`                  |
| Empty answer                       | Print full `response` map to inspect shape |

## Learn more

* [**Sessions**](https://dnotifier.gitbook.io/product-docs/ai/sessions/dart-flutter.md) — `sessionId` continuation
* [**Chat-style messages**](https://dnotifier.gitbook.io/product-docs/ai/chat-style-messages/dart-flutter.md) — `messages` array
