SDKs

Dart / Flutter

Idiomatische Dart-patronen voor Flutter-apps en Dart-server

Gebruik het http-package voor eenvoudige gevallen of dio voor interceptors en retries. Hieronder wordt http getoond — deze wordt out of the box meegeleverd met de Dart SDK voor Flutter.

pubspec.yaml

dependencies:
  http: ^1.2.2

Configureren

import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

const api = 'https://openapi.thunderbit.com/openapi/v1';
final headers = <String, String>{
  'Authorization': 'Bearer ${Platform.environment['THUNDERBIT_API_KEY']}',
  'Content-Type': 'application/json',
};

Een pagina Distillen

Future<String> distill(String url) async {
  final res = await http.post(
    Uri.parse('$api/distill'),
    headers: headers,
    body: jsonEncode({'url': url}),
  );
  if (res.statusCode >= 400) throw Exception(res.body);
  final body = jsonDecode(res.body) as Map<String, dynamic>;
  return body['data']['markdown'] as String;
}

Gestructureerde data Extracten

Future<Map<String, dynamic>> extract(String url) async {
  final res = await http.post(
    Uri.parse('$api/extract'),
    headers: headers,
    body: jsonEncode({
      'url': url,
      'schema': {
        'type': 'object',
        'properties': {
          'name':  {'type': 'string'},
          'price': {'type': 'number'},
        },
        'required': ['name', 'price'],
      },
    }),
  );
  return (jsonDecode(res.body) as Map<String, dynamic>)['data'];
}

Flutter UI-tip

Wikkel calls in een FutureBuilder zodat de UI niet blokkeert — en laat je Dart-server (niet de Flutter-app) de API Key beheren in productie.

Voor batch jobs submit je en laat je je server-side Webhook handler het werk doen. Zie Webhooks.

Een officiële Dart SDK is in ontwikkeling — kom binnenkort terug.