> 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/caf-sdk-pt-br/flutter/standalone-modules/documentdetector/source-code.md).

# Código-fonte

O código-fonte abaixo também pode ser visto no [Repositório Flutter FaceAuth da Caf](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("SUCESSO");
        print("Tipo de documento: ${event.documentType}");
        for (var capture in event.captures!) {
          print("""
              Rótulo do documento: ${capture.label ?? "vazio"}
              Qualidade da imagem: ${capture.quality ?? "vazio"}
              Caminho do arquivo: ${capture.imagePath ?? "vazio"}
              URL do arquivo: ${capture.imageUrl?.split("?")[0] ?? "vazio"}
            """
          );
        }
      } else if (event is DocumentDetectorEventFailure) {
        print("FALHA");
        print("""
          Tipo de falha: ${event.errorType ?? "vazio"}
          Descrição da falha: ${event.errorMessage ?? "vazio"}
          Código de segurança: ${event.securityErrorCode ?? "nenhum"}
         """
        );
      } else if (event is DocumentDetectorEventClosed) {
        print("FECHADO: O usuário fechou o fluxo de captura de documento.");
      }
    } on PlatformException catch (e) {
      print("ERRO: ${e.message}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Demonstração do DocumentDetector'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _startDocumentDetector,  // Acionar a detecção ao pressionar o botão
          child: const Text("Iniciar detecção de documento"),
        ),
      ),
    );
  }
}
```


---

# 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/caf-sdk-pt-br/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.
