搜索插件、代码仓与 APP 分类

插件详情v1.0.1

fitness

用于读取步数数据的 Flutter 插件。在 iOS 上封装 HealthKit,在 Android 上封装 GoogleFit。

AndroidiOS
近 30 天下载
50
喜欢数
29
Pub 分数
140

安装插件

flutter pub add fitness

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

健身包是一个Flutter插件,可从原生健康平台访问步数数据。在iOS上,它与HealthKit集成;在Android上,它连接到Google Fit。这使得开发者能够以跨平台的方式获取步数信息,简化了健康数据在健身或健康应用中的集成。该插件会自动处理平台特定的设置和权限,提供跨设备的一致API。

适合场景

  • 跟踪每日步数
  • 构建健身仪表板
  • 与健康分析集成
  • 启用用户活动目标
  • 支持健康应用

主要特点

  • 跨平台步数访问
  • HealthKit (iOS) 集成
  • Google Fit(Android)集成
  • 自动权限处理
  • 用于获取步数数据的简单API

适合对象

  • 健身应用开发者
  • 健康应用程序创作者
  • 健康数据集成商
  • 面向 iOS 和 Android 的移动开发者

使用建议

  • 需要在两个平台上获取用户权限
  • 需要在 iOS 和 Android 清单中进行正确配置
  • 仅限步数(不包括其他健康指标)
  • 取决于用户是否启用了健康Kit或Google Fit
  • 可能需要设置后台执行以实现持续跟踪

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Fitness

pub package

Flutter plugin for reading step count data. Wraps HealthKit on iOS and GoogleFit on Android.

Related Document

:point_right: Korean

Getting Started

Check out the example directory for a sample app.

Add pubspec.yaml

To use this plugin, add fitness as a dependency in your pubspec.yaml file.

Android Integration

Enable Fitness API and obtain an OAuth 2.0 client ID.

iOS Integration

Enable HealthKit and add NSHealthShareUsageDescription key to the Info.plist file.

Usage

Fitness.hasPermission

Check if the user has previously granted the necessary data access.

Note: Quite rightly so, Apple deems even the information as to whether a user accepted or denied the read permission for HealthKit as sensitive information. For this reason, HealthKit does not have a clear way to check permissions.

As a workaround, it checks whether you can read data within the last month or not to see if you actually have read access.

void _hasPermission() async {
  final result = await Fitness.hasPermission();
}
Fitness.requestPermission

Initiate the authorization flow.

  • Android: Request OAuth permission and request permission to read step count history from Google Fit. It also creates a subscription to synchronize step count data.

  • iOS: Request permission to read step count history from Health Kit. Since it returns whether the request for HKHealthStore.requestAuthorization was successful or not, it is recommended to use Fitness.hasPermission in the case of iOS to check if there is permission again.

void _requestPermission() async {
  final result = await Fitness.requestPermission();
}
Fitness.revokePermission
  • Android: All OAuth permissions granted to Googlt Fit are revoked and all subscriptions created in the app are removed.

  • iOS: Feature is not supported, so it always returns true.

void _revokePermission() async {
  final result = await Fitness.revokePermission();
}
Fitness.read

Requests user's step data with Google Fit or Health Kit.

When requested without any arguments, the daily step count is requested by default.

Parameters
  • timeRange
    • defaults: from 00:00:00 to 23:59:59
    • type: TimeRange [class]
  • bucketByTime
    • defaults: 1
    • type: int
  • timeUnit
    • defaults: TimeUnit.day
    • type: TimeUnit [enum]
// Request data for the last 7 days.
void _read() async {
  final now = DateTime.now();
  final results = await Fitness.read(
    timeRange: TimeRange(
      start: now.subtract(const Duration(days: 7)),
      end: now,
    ),
    bucketByTime: 1,
    timeUnit: TimeUnit.days,
  );
}

所属分类

源码仓库

使用此插件的项目

暂无相关数据。