> 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/example-rag-bot/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

Index FAQ content, then answer questions with **`useKnowledgeBase: true`**.

## Step 1 — Index documents

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

const indexerId = 'kb-indexer';

Future<void> indexFaqs(DNotifier notifier) async {
  final faqs = [
    {
      'recordId': 'faq-refund',
      'content': 'Refunds are processed within 5 business days after approval.',
    },
    {
      'recordId': 'faq-password',
      'content':
          'Reset your password under Settings → Security → Reset password.',
    },
    {
      'recordId': 'faq-shipping',
      'content': 'Standard shipping takes 3–7 business days domestically.',
    },
  ];

  for (final faq in faqs) {
    await notifier.addDocument(
      senderId: indexerId,
      recordId: faq['recordId']!,
      content: faq['content']!,
      type: 'faq',
      metadata: {'source': 'help-center'},
    );
  }
  print('Indexed ${faqs.length} documents');
}

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

## Step 2 — RAG Q\&A function

```dart
final sessions = <String, String>{};

Future<String> askSupport(
  DNotifier notifier,
  String userId,
  String question,
) async {
  final response = await notifier.sendAI(
    senderId: userId,
    sessionId: sessions[userId],
    message: {
      'useKnowledgeBase': true,
      'messages': [
        {
          'role': 'system',
          'content':
              'You are a support bot. Answer only from retrieved knowledge. '
              'If the answer is not in the knowledge base, say you do not know.',
        },
        {'role': 'user', 'content': question},
      ],
    },
    saveHistory: true,
  );

  final sid = _sessionIdFromResponse(response);
  if (sid != null && !sessions.containsKey(userId)) {
    sessions[userId] = sid;
  }

  return _extractContent(response);
}

String _extractContent(dynamic response) {
  if (response is Map && response['data'] is Map) {
    return response['data']['content']?.toString() ?? '';
  }
  return response.toString();
}

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

## Step 3 — Manual search variant

```dart
Future<Map<String, dynamic>> askSupportManual(
  DNotifier notifier,
  String userId,
  String question,
) async {
  final hits = await notifier.search(
    senderId: userId,
    query: question,
    limit: 5,
    minSimilarity: 0.5,
    filterbySource: 'help-center',
  );

  final response = await notifier.sendAI(
    senderId: userId,
    message: {
      'messages': [
        {'role': 'system', 'content': 'Context:\n$hits'},
        {'role': 'user', 'content': question},
      ],
    },
  );

  return {
    'answer': _extractContent(response),
    'sources': hits,
  };
}
```

## Learn more

* [**Semantic search**](https://dnotifier.gitbook.io/product-docs/ai/semantic-search/dart-flutter.md)
* [**Intent router workflow**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-intent-router/dart-flutter.md)
