> 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/workflows-and-agents/example-intent-router/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

Self-contained intent router — copy into a single Dart file.

## Complete runnable example

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

// ── Helpers ─────────────────────────────────────────────────────────

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

// ── Agents ──────────────────────────────────────────────────────────

final intentAgent = defineAgent(
  name: 'intent-agent',
  run: (ctx) async {
    final input = ctx.input;
    final text = input is String
        ? input
        : (input is Map
            ? (input['text'] ?? input['question'] ?? '').toString()
            : input.toString());

    if (text.trim().isEmpty) {
      throw WorkflowError('intent-agent: empty input', agentName: 'intent-agent');
    }

    final aiResponse = await ctx.sendAI(
      message: {
        'useKnowledgeBase': false,
        'messages': [
          {
            'role': 'system',
            'content':
                'Classify the user message. Reply with JSON only: {"intent":"search"} for documentation/product lookup questions, or {"intent":"general"} for general chat.',
          },
          {'role': 'user', 'content': text},
        ],
      },
      saveHistory: false,
      label: 'Intent classification',
    );

    final content = extractContent(aiResponse);
    Map<String, dynamic> parsed;
    try {
      parsed = jsonDecode(content) as Map<String, dynamic>;
    } catch (_) {
      parsed = {
        'intent': content.toLowerCase().contains('search') ? 'search' : 'general',
      };
    }

    final intent = parsed['intent'] == 'search' ? 'search' : 'general';
    ctx.state['lastIntent'] = intent;
    return {'intent': intent, 'raw': parsed};
  },
);

final generalAgent = defineAgent(
  name: 'general-agent',
  run: (ctx) async {
    final input = ctx.input;
    final question = input is String
        ? input
        : (input is Map ? input['question']?.toString() : input.toString());

    final aiResponse = await ctx.sendAI(
      message: {
        'useKnowledgeBase': false,
        'messages': [
          {'role': 'system', 'content': 'You are a helpful assistant. Answer concisely.'},
          {'role': 'user', 'content': question ?? ''},
        ],
      },
      saveHistory: false,
      label: 'General Q&A',
    );

    final answer = extractContent(aiResponse);
    ctx.state['generalAnswer'] = answer;
    return {'answer': answer};
  },
);

// ── Workflow ────────────────────────────────────────────────────────

Future<dynamic> intentRouterEntry(WorkflowContext ctx) async {
  final intentResult = await ctx.runAgent('intent-agent');
  final intent =
      (intentResult is Map ? intentResult['intent'] : null) ?? 'general';
  ctx.state['intent'] = intent;

  if (intent == 'search') {
    final input = ctx.input;
    final query = input is String
        ? input
        : (input is Map ? (input['query'] ?? input['text'] ?? '').toString() : input.toString());

    final hits = await ctx.search(
      query: query,
      limit: 10,
      minSimilarity: 0.3,
      filterbySource: 'help-center',
      label: 'Semantic search',
    );
    ctx.state['searchResults'] = hits;
    return {'branch': 'search', 'intent': intentResult, 'hits': hits};
  }

  final enrichedInput = {
    'question': ctx.input is String ? ctx.input : ctx.input,
    'intentMeta': intentResult,
  };
  final generalResult =
      await ctx.runAgent('general-agent', input: enrichedInput);

  return {'branch': 'general', 'intent': intentResult, 'answer': generalResult};
}

final intentRouterWorkflow = Workflow(
  name: 'support-intent-router',
  description: 'Classify intent then search or general chat',
  observability: true,
  entry: intentRouterEntry,
)
  ..registerAgents({
    'intent-agent': intentAgent,
    'general-agent': generalAgent,
  });

// ── Runner ──────────────────────────────────────────────────────────

const userId = 'workflow-runner';

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

  await notifier.connect();

  final searchRun = await notifier.runWorkflow(
    workflow: intentRouterWorkflow,
    input: 'How do refunds work?',
  );
  print('Search branch: ${searchRun.result} ${searchRun.executionId}');

  final generalRun = await notifier.runWorkflow(
    workflow: intentRouterWorkflow,
    input: 'What is your name?',
  );
  print('General branch: ${generalRun.result} ${generalRun.executionId}');
}
```

## Learn more

* [**Define an agent**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/define-agent/dart-flutter.md)
* [**RAG bot example**](https://dnotifier.gitbook.io/product-docs/ai/example-rag-bot/dart-flutter.md)
