Integrating HealthKit/Google Fit in Flutter: A Comprehensive Guide

Quick Summary: The blog provides a comprehensive guide on how to integrate HealthKit for iOS and Google Fit for Android into a Flutter application. It includes step-by-step instructions on adding dependencies, configuring permissions, and using relevant plugins to fetch health and fitness data. The blog emphasizes the benefits of accessing health data to enhance user experience and create cross-platform health-oriented applications.

What is HealthKit and Google Fit?

HealthKit is a framework provided by Apple for iOS devices that allows apps to store and retrieve health and fitness data from the Health app.

Google Fit is a health-tracking platform developed by Google for Android devices that provides similar functionalities for storing and managing fitness and health data.

Integrating HealthKit and Google Fit with Flutter

To integrate health data from HealthKit (iOS) or Google Fit (Android) into your Flutter application, you will typically use platform-specific APIs and plugins that bridge the gap between Flutter and native code.

CTA Flutter

Integrating HealthKit (iOS)

1. Add Dependencies

You need to use a plugin that provides access to HealthKit data. The health_kit_reporter plugin can be used for HealthKit integration. Add the plugin to your pubspec.yaml:

dependencies:

  flutter:

    sdk: flutter

  health_kit_reporter: ^0.3.1  # Check for the latest version

Run flutter pub get to install the dependency.

2. Configure iOS Permissions

You need to request permissions to access HealthKit data. Open the ios/Runner/Info.plist file and add the necessary permissions:

<key>NSHealthShareUsageDescription</key>

<string>We need access to your health data to provide better fitness tracking.</string>

<key>NSHealthUpdateUsageDescription</key>

<string>We need to update your health data to keep it accurate.</string>

3. Use HealthKit in Your Flutter App

Here’s an example of how to use the health_kit_reporter plugin to fetch health data:

import 'package:flutter/material.dart';

import 'package:health_kit_reporter/health_kit_reporter.dart';

void main() {

  runApp(MyApp());

}

class MyApp extends StatefulWidget {

  @override

  _MyAppState createState() => _MyAppState();

}

class _MyAppState extends State<MyApp> {

  String _healthData = 'No data';

  @override

  void initState() {

    super.initState();

    _fetchHealthData();

  }

  Future<void> _fetchHealthData() async {

    final healthKit = HealthKitReporter();

    final healthData = await healthKit.getStepsData();

    setState(() {

      _healthData = 'Steps: $healthData';

    });

  }

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      home: Scaffold(

        appBar: AppBar(title: Text('HealthKit Integration')),

        body: Center(

          child: Text(_healthData),

        ),

      ),

    );

  }

}

Integrating Google Fit (Android)

1. Add Dependencies

For Google Fit integration, use the google_fit plugin. Add it to your pubspec.yaml:

dependencies:

  flutter:

    sdk: flutter

  google_fit: ^1.1.0  # Check for the latest version

Run flutter pub get to install the dependency.

2. Configure Android Permissions

Update android/app/src/main/AndroidManifest.xml to include the necessary permissions:

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Also, make sure your build.gradle file is configured with the appropriate dependencies:

dependencies {

  implementation 'com.google.android.gms:play-services-fitness:21.0.1'

}

3. Use Google Fit in Your Flutter App

Here’s an example of how to use the google_fit plugin to fetch step count data:

import 'package:flutter/material.dart';

import 'package:google_fit/google_fit.dart';

void main() {

  runApp(MyApp());

}

class MyApp extends StatefulWidget {

  @override

  _MyAppState createState() => _MyAppState();

}

class _MyAppState extends State<MyApp> {

  String _fitData = 'No data';

  @override

  void initState() {

    super.initState();

    _fetchFitData();

  }

  Future<void> _fetchFitData() async {

    final googleFit = GoogleFit();

    final stepCount = await googleFit.getSteps();

    setState(() {

      _fitData = 'Steps: $stepCount';

    });

  }

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      home: Scaffold(

        appBar: AppBar(title: Text('Google Fit Integration')),

        body: Center(

          child: Text(_fitData),

        ),

      ),

    );

  }

}

Summary

  1. HealthKit Integration (iOS):
    • Add the health_kit_reporter plugin.
    • Configure permissions in Info.plist.
    • Use the plugin to fetch health data.
  2. Google Fit Integration (Android):
    • Add the google_fit plugin.
    • Configure permissions and dependencies.
    • Use the plugin to fetch health data.

Conclusion

Integrating HealthKit and Google Fit into a Flutter application allows you to access and utilize health and fitness data, providing valuable insights and enhancing user experience. By leveraging these integrations, you can create health-oriented applications that work seamlessly across both iOS and Android platforms.

Get free consultation from the best flutter development company in india to elevate your Flutter app design. Unlock the full potential of Flutter layouts with our professional Flutter developers. 

Remote Team