Zum Inhalt

Flutter / Dart Beispiele – Health Fitness API

Verwenden Sie Dart in Ihren Flutter-Apps, um auf die Health Fitness API zuzugreifen. Die folgenden Beispiele verwenden das Paket http und dio.


1. Verwendung des Pakets http

Fügen Sie die Abhängigkeit in pubspec.yaml hinzu:

dependencies:
  http: ^1.1.0

Beispiel für eine GET-Anfrage:

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

void main() async {
  final apiKey = "YOUR_API_KEY";
  final url = Uri.parse("https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en");

  final response = await http.get(
    url,
    headers: {"X-API-Key": apiKey},
  );

  if (response.statusCode == 200) {
    final data = json.decode(response.body);
    print(data);
  } else {
    print("Request failed with status: ${response.statusCode}");
  }
}

Beispiel für eine POST-Anfrage:

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

void main() async {
  final apiKey = "YOUR_API_KEY";
  final url = Uri.parse("https://api.hefitapi.com/api/v1/bmi/post");

  final payload = json.encode({"height": 178, "weight": 82, "units": "metric"});

  final response = await http.post(
    url,
    headers: {
      "Content-Type": "application/json",
      "X-API-Key": apiKey
    },
    body: payload,
  );

  if (response.statusCode == 200) {
    final data = json.decode(response.body);
    print(data);
  } else {
    print("Request failed with status: ${response.statusCode}");
  }
}

2. Verwendung des Pakets dio

Fügen Sie die Abhängigkeit in pubspec.yaml hinzu:

dependencies:
  dio: ^6.1.0

Beispiel für eine GET-Anfrage:

import 'package:dio/dio.dart';

void main() async {
  final dio = Dio();
  final apiKey = "YOUR_API_KEY";
  final url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

  final response = await dio.get(
    url,
    options: Options(headers: {"X-API-Key": apiKey}),
  );

  print(response.data);
}

Beispiel für eine POST-Anfrage:

import 'package:dio/dio.dart';

void main() async {
  final dio = Dio();
  final apiKey = "YOUR_API_KEY";
  final url = "https://api.hefitapi.com/api/v1/bmi/post";

  final payload = {"height": 178, "weight": 82, "units": "metric"};

  final response = await dio.post(
    url,
    data: payload,
    options: Options(headers: {"X-API-Key": apiKey}),
  );

  print(response.data);
}

Andere Sprachen

Erfahren Sie, wie Sie die Health Fitness API in anderen Programmiersprachen integrieren können:

Beginnen Sie noch heute mit der Integration der Health Fitness API in Ihre mobilen und Web-Apps!


Health Fitness API Ermöglicht die moderne digitale Gesundheitsinfrastruktur