SDKs

Dart / Flutter

Pattern Dart idiomatici per app Flutter e server Dart

Usa il pacchetto http per i casi semplici o dio per interceptor e retry. Qui sotto è mostrato http — viene fornito out of the box con il Dart SDK per Flutter.

pubspec.yaml

dependencies:
  http: ^1.2.2

Configurazione

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',
};

Distill di una pagina

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;
}

Extract di dati strutturati

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'];
}

Suggerimento per UI Flutter

Avvolgi le chiamate in un FutureBuilder così la UI non si blocca — e lascia che il tuo server Dart (non l'app Flutter) detenga l'API Key in produzione.

Per i batch job, fai il submit e poi lascia che il tuo handler Webhook lato server faccia il lavoro. Vedi Webhooks.

Un SDK Dart ufficiale è in sviluppo — torna presto a controllare.