# Getting started

# Step 1: Install Fitbitter

To install Fitbitter, simply add fitbitter: to the dependencies of your pubspec.yaml file:

dependencies:
    fitbitter: #latest version

# (for Android only) Modify you manifest

Fitbitter uses flutter_web_auth to let you authenticate to Fitbit. In Android, you need to add these lines of code to your android/app/src/main/AndroidManifest.xml file:

<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
      <intent-filter android:label="flutter_web_auth">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="YOUR_CALLBACK_SCHEME" />
      </intent-filter>
    </activity>

and change YOUR_CALLBACK_SCHEME with your callback scheme (in the test example below this will be example)

# Step 2: Test Fitbitter

Once installed, it is time to test drive Fitbitter. In this example, we will use Fitbitter to authenticate our app into Fitbit APIs and simply fetch yesterday's step count.

# Preliminary requirement: Register your app

To be able to perform any operation with the Fitbit APIs, you must register first your application in the developer portal of Fitbit and obtain two IDs, namely the "OAuth 2.0 Client ID" and the "Client Secret". To do so, you have to follow these steps.

  • Create a Fitbit account, if you do not have one.

  • Register a new app at https://dev.fitbit.com/apps/new (opens new window). The form will look like this: Fitbit App Registration Form

  • Choose an "Application Name" (e.g., "Example").

  • Set a brief "Description" (e.g., "Just a simple test of an awesome package.")

  • Set the "Application Website". It can be random for the purpose of testing the example so let's put https://www.example.com/.

  • Set an "Organization" (e.g., "Myself").

  • Set an "Organization Website". It can be random for the purpose of testing the example so let's put https://www.myself.com/.

  • Set a "Terms of Service Url". It can be random for the purpose of testing the example so let's put https://www.myself.com/tos.

  • Set an "Privacy Policy Url". It can be random for the purpose of testing the example so let's put https://www.myself.com/pp.

  • Set the "Application Type" to "Client".

  • Choose a Callback URL" (e.g., "example://fitbit/auth").

  • Choose a "Default Access Type" (e.g., "Read-Only").

  • Check the "I have read and agree to the terms of service" box.

  • Click the "Register" button. You should now see the "OAuth 2.0 Client ID" and the "Client Secret" strings provided by Fitbit.

# App authentication

You are now ready to authorize your application.

To do that, simply call the asynchronous method FitbitConnector.authorize(), within your code, as:

    String? userId = await FitbitConnector.authorize(
        context: context,
        clientID: '<OAuth 2.0 Client ID>',
        clientSecret: '<Client Secret>',
        redirectUri: '<Redirect Uri used during the registration, e.g., example://fitbit/auth>',
        callbackUrlScheme: '<Callback Uri scheme, e.g., example>');

This will open a web view where user will be able to input his Fitbit credentials and login. After the login, the web view will close and the method will return a String? containing the userId.

WARNING

Don't worry about storing the auth tokens provided by the Fitbit API, Fitbitter will do that for you leveraging the SharedPreferences abd GetIt packages. If you want, you can access to the token values using

GetIt.instance<SharedPreferences>().getString("fitbitAccessToken")
GetIt.instance<SharedPreferences>().getString("fitbitRefreshToken")

TIP

Remember to retain userId: it will be used to fetch data.

# Fetch step count data

With your app authorized, you are now ready to fetch data from Fitbit. In this example, we will fetch yesterday's step count.

Using Fitbitter, this is very easy. First instanciate a FitbitActivityTimeseriesDataManager of type: 'steps':

    FitbitActivityTimeseriesDataManager
        fitbitActivityTimeseriesDataManager =
        FitbitActivityTimeseriesDataManager(
            clientID: '<OAuth 2.0 Client ID>',
            clientSecret: '<Client Secret>',
            type: 'steps', 
        );

Then fetch the desired data using the fetch method of FitbitActivityTimeseriesDataManager with the proper FitbitActivityTimeseriesAPIURL:

final stepsData = await fitbitActivityTimeseriesDataManager.fetch(
    FitbitActivityTimeseriesAPIURL.dayWithResource(
        date: DateTime.now().subtract(Duration(days: 1)),
        userID: fitbitAccount.id,
        resource: fitbitActivityTimeseriesDataManager.type,
        )
    ) as List<FitbitActivityTimeseriesData>;

That's it!

TIP

For more information on Fitbitter functionalities, check out the Guides and the Fitbitter Reference API (opens new window)