Q: How to access Apple Data with Flutter?

LinearGoat2 months ago

How can I retrieve data from Apple watch to my flutter app?

terra

Elliottadmin2 months ago

Hey there!

In order to access data through our Apple integration, you can use our native iOS SDK, or cross-platform Flutter or React Native solutions.

To start developing using the SDK with Flutter, you will have to run the following sequence of functions (or the equivalent in other SDKs):

  1. Setup your project using the guides above
  2. Create a method to generate an authentication token that can be used to connect the iOS user to Terra like so (note that you will have to create an endpoint to generate the auth token by calling this endpoint)
Future<String> getAuthToken() async { var response = await http.get(Uri.parse("$yourApiBackend/authToken")); if (response.statusCode == 200) { var data = jsonDecode(response.body); return data["token"]; } return ""; }
  1. Next use the token to initialise the connection:
TerraFlutter.initTerra("YOUR-DEV-ID", "YOUR-USER-ID").then((value) async { log("Init Status => ${value?.success} ${value?.error}"); var token = await getAuthToken(); await TerraFlutter.initConnection(Connection.appleHealth, token, true, []).then((value) async { log("Connect Status => ${value?.success} ${value?.error}"); }); // fetches historical data await TerraFlutter.getNutrition(Connection.appleHealth, DateTime(2023, 10, 01), DateTime(2023, 10, 10), toWebhook: true).then((value) async { log("Get Payload Status => ${value?.success} ${value?.error}"); }); });

Hope this has clarified things :)


LinearGoat2 months ago

Thanks!