> For the complete documentation index, see [llms.txt](https://docs.caf.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.caf.io/caf-sdk/flutter/standalone-modules/documentdetector/source-code.md).

# Source Code

The source code below can also be seen in the [Caf's FaceAuth Flutter Repository](https://github.com/combateafraude/FaceAuthenticatorFlutter/blob/main/face_auth_example/lib/main.dart)

```dart
import 'package:caf_document_detector/android_settings/android_settings.dart';
import 'package:caf_document_detector/android_settings/security_settings.dart';
import 'package:caf_document_detector/document_capture_flow.dart';
import 'package:caf_document_detector/document_detector.dart';
import 'package:caf_document_detector/document_detector_events.dart';
import 'package:caf_document_detector/enums.dart';
import 'package:caf_document_detector/upload_settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: DocumentDetectorDemo(),
    );
  }
}

class DocumentDetectorDemo extends StatefulWidget {
  const DocumentDetectorDemo({Key? key}) : super(key: key);

  @override
  DocumentDetectorDemoState createState() => DocumentDetectorDemoState();
}

class DocumentDetectorDemoState extends State<DocumentDetectorDemo> {
  late final DocumentDetector documentDetector;

  @override
  void initState() {
    super.initState();
    documentDetector = buildDocumentDetector();
  }

  DocumentDetector buildDocumentDetector() {
    const String mobileToken = "mobile_token";
    const String personId = "person_id";

    DocumentDetector documentDetector = DocumentDetector(
      mobileToken: mobileToken,
      captureFlow: [
        DocumentCaptureFlow(documentType: DocumentType.cnhFront),
        DocumentCaptureFlow(documentType: DocumentType.cnhBack),
      ],
    );

    UploadSettings uploadSettings = UploadSettings();

    AndroidSettings androidSettings = AndroidSettings(
        securitySettings: SecuritySettings(
            useAdb: false,
            useDebug: false,
            useDeveloperMode: false,
            useEmulator: false,
            useRoot: false
        )
    );


    documentDetector.setAndroidSettings(androidSettings);
    documentDetector.setUploadSettings(uploadSettings);
    documentDetector.setPersonId(personId);
    documentDetector.setStage(CafStage.prod);

    return documentDetector;
  }

  void _startDocumentDetector() async {
    try {
      DocumentDetectorEvent event = await documentDetector.start();

      if (event is DocumentDetectorEventSuccess) {
        print("SUCCESS");
        print("Document Type: ${event.documentType}");
        for (var capture in event.captures!) {
          print("""
              Document Label: ${capture.label ?? "empty"}
              Image Quality: ${capture.quality ?? "empty"}
              File Path: ${capture.imagePath ?? "empty"}
              File URL: ${capture.imageUrl?.split("?")[0] ?? "empty"}
            """
          );
        }
      } else if (event is DocumentDetectorEventFailure) {
        print("FAILURE");
        print("""
          Failure Type: ${event.errorType ?? "empty"}
          Failure Description: ${event.errorMessage ?? "empty"}
          Security Code: ${event.securityErrorCode ?? "none"}
         """
        );
      } else if (event is DocumentDetectorEventClosed) {
        print("CLOSED: User closed document capture flow.");
      }
    } on PlatformException catch (e) {
      print("ERROR: ${e.message}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DocumentDetector Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _startDocumentDetector,  // Trigger detection on button press
          child: const Text("Start Document Detection"),
        ),
      ),
    );
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.caf.io/caf-sdk/flutter/standalone-modules/documentdetector/source-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
