Google Fit with fit_kit package!

Harshdeep Singh
4 min readSep 16, 2020

NOTE:
The fitkit package is now in the unmaintained section. Do check out the Health package for now or you can use the same package for testing purpose, also there are extension of this package available. The setup part is similar for all the packages we use for Google fit store and iOS Health Store.

As new technology emerges day by day, I want to explore the ways we can use the Mobile device itself as a parameter to work with Health Telemetrics. This article contains an overview of Google Fit APIs and how I gonna use them in Flutter.

What is Google Fit?

Google Fit is an open ecosystem which lets developer or user upload health and wellness data to a central repository where a user access their data from different devices or app. Since it is a central repository, a user can access data even if they change their device as the data will be linked to Google Account.

Google Fit itself an app on AppStore and Play store by Google. Google Fit provides a single set of APIs for apps and access activity data from fitness apps and sensors on Android and other devices (like wearables, heart rate monitors, or connected scales).

Getting Started with Google Fit on Flutter

To get health data from Google Fit, I have used a Flutter package called fit_kit.

In pubspec.yaml add dependancy as below

dependencies:
fit_kit: <latest-version>

- To run the fit_kit package on the android need to enable Google fitness API from Google console & obtain OAuth 2.0 Client ID. Permission required -ACTIVITY_RECOGNITION for sdk >= 28.

- For iOS, the plugin contains HealthKit configuration as provided by Apple itself. One can set up HealthKit from here.

Permission required — NSHealthShareUsageDescription to access Health share data by HealthKit

- To get data from Google Fit on the iOS device, the Google Fit app needs to be installed. In the case of Android devices, there is no need to install the fit app as one can work without it too.

IMPLEMENTATION

The fit_kit package provides access to data in two ways: -
1. To access the specific type of DataType as provided
2. Or, to access all type of DataType as provided for a given platform.

First thing first… Ask the user for permission to access data.

The hasPerissiom method checks if the user has given permission or not for the specified data types.

await FitKit.hasPermissions([ DataType.HEART_RATE, DataType.STEP_COUNT, DataType.HEIGHT, DataType.WEIGHT, DataType.DISTANCE, DataType.ENERGY, DataType.WATER ]);

The requestPermissions methods ask the user to provide permission for specified data types.

await FitKit.hasPermissions([ DataType.HEART_RATE, DataType.STEP_COUNT, DataType.HEIGHT, DataType.WEIGHT, DataType.DISTANCE, DataType.ENERGY, DataType.WATER ]);

As soon as, user all the permission one can access data as below:-

await FitKit.read( type, dateFrom: dateFrom, dateTo: dateTo);

The read method needs three parameters to get data from Google Fit.
a. type — The type of data one wants to access in fit_kit.
b. dateFrom — From which date-time one wants to access the data.
c. dateTo — To which date-time one wants to access the data.

Example

Here is the screenshot of the data accessed via Google Fit: -

In the screenshot, different DataType is displayed with the session given From date-time, To date-time, and the value consumed with its display unit.

Sources for different DataType

  • DataType.HEART_RATE: For this data, I used an app called HEART RATE MONITOR, which records the heart rate through the app and sends data to the Google Fit repository.
  • DataType.HEIGHT & DataType.WEIGHT: For this data, devices which measure Height and Weight sends data to Google Fit.
  • DataType.STEP_COUNTER & DataType.DISTANCE: The sensor of the android is used to detect steps and sent to Google Fit Repository.
  • DataType.Water: For this data, I used water reminding app called Hydrillo.

Resources:-

a. Google Fit ( https://developers.google.com/fit/ )
b. fit_kit ( https://pub.dev/packages/fit_kit )
c. Project Source ( https://github.com/Harshdeep4295/HealthCheckup )

I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.

Happy coding! Thanks for reading!

NOTE:
If the app keeps after the user selects google login. Then in the GCP add test user as shown in this image as the app is in a resting state.

--

--

Harshdeep Singh

Mobile Developer currently working at One Impression. Tech Enthusiast.