> ## Documentation Index
> Fetch the complete documentation index at: https://docs.markpdf.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart / Flutter

> http package to convert documents from Dart and Flutter.

# Dart / Flutter

## Dependencia

```yaml theme={null}
# pubspec.yaml
dependencies:
  http: ^1.2.0
```

## Servicio

```dart theme={null}
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

class FlashMdClient {
  final String apiUrl;
  final String apiKey;

  FlashMdClient({required this.apiUrl, required this.apiKey});

  Future<String> convert(File file) async {
    final filename = file.uri.pathSegments.last;
    final bytes = await file.readAsBytes();

    final res = await http.post(
      Uri.parse('$apiUrl/convert/raw?filename=$filename&mode=fast'),
      headers: {
        'x-api-key': apiKey,
        'content-type': 'application/pdf',
      },
      body: bytes,
    );

    if (res.statusCode == 200) return res.body;

    if (res.statusCode == 202) {
      final job = jsonDecode(res.body);
      return _pollJob(job['job_id'] as String);
    }

    throw Exception('HTTP ${res.statusCode}: ${res.body}');
  }

  Future<String> convertFromUrl(String url, String filename) async {
    final res = await http.post(
      Uri.parse('$apiUrl/convert/from-url'),
      headers: {
        'x-api-key': apiKey,
        'content-type': 'application/json',
      },
      body: jsonEncode({'url': url, 'filename': filename, 'mode': 'fast'}),
    );

    if (res.statusCode == 200) return res.body;

    if (res.statusCode == 202) {
      final job = jsonDecode(res.body);
      return _pollJob(job['job_id'] as String);
    }

    throw Exception('HTTP ${res.statusCode}: ${res.body}');
  }

  Future<String> _pollJob(String jobId) async {
    while (true) {
      await Future.delayed(const Duration(seconds: 5));

      final res = await http.get(
        Uri.parse('$apiUrl/jobs/$jobId'),
        headers: {'x-api-key': apiKey},
      );

      final data = jsonDecode(res.body);

      if (data['status'] == 'completed') return data['body'] as String;
      if (data['status'] == 'failed') throw Exception(data['error']);
    }
  }
}
```

## Uso en Flutter

```dart theme={null}
final client = FlashMdClient(
  apiUrl: 'https://api.markpdf.tech',
  apiKey: const String.fromEnvironment('FLASH_MD_API_KEY'),
);

// From local file
final markdown = await client.convert(File('/path/to/report.pdf'));

// From URL signed
final md = await client.convertFromUrl(
  'https://bucket.s3.amazonaws.com/doc.pdf?X-Amz-Signature=...',
  'doc.pdf',
);
```

<Warning>
  Do not put the API key directly in a published mobile app. Use your own backend as a proxy.
</Warning>
