コンテンツにスキップ

Flutter / Dart の例 – ヘルス フィットネス API

Dart を使用して、Flutter アプリ からヘルス フィットネス API にアクセスします。以下に、http パッケージと dio を使用した例を示します。


1. http パッケージの使用

依存関係を pubspec.yaml に追加します。

dependencies:
  http: ^1.1.0

GET リクエストの例:

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

POST リクエストの例:

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. dio パッケージの使用

依存関係を pubspec.yaml に追加します。

dependencies:
  dio: ^6.1.0

GET リクエストの例:

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

POST リクエストの例:

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

その他の言語

ヘルス フィットネス API を他のプログラミング言語で統合する方法を調べてください。

ヘルス フィットネス API を今日からモバイルおよび Web アプリに統合しましょう!


ヘルス フィットネス API 現代のデジタルヘルスインフラを支える