my_hk_reader 2.0.6 copy "my_hk_reader: ^2.0.6" to clipboard
my_hk_reader: ^2.0.6 copied to clipboard

custom HealthKit reader plugin.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:my_hk_reader/my_hk_reader.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  double steps = 0;
  double distances = 0;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      // platformVersion = await MyHkReader.getPlatformVersion();

      await MyHkReader.requestPermissions(
          [DataType.STEP_COUNT, DataType.DISTANCE]);
      var result = await MyHkReader.readSteps(DataType.STEP_COUNT,
          dateFrom: DateTime.now().subtract(Duration(days: 2)),
          dateTo: DateTime.now());
      // print(" ================ result ============= ${result}");
      // print(" ================ result runtimeType ============= ${result.runtimeType}");
      var resultDistances = await MyHkReader.readDistances(DataType.DISTANCE,
          dateFrom: DateTime.now().subtract(Duration(days: 2)),
          dateTo: DateTime.now());
      setState(() {
        steps = result[0]['value'];
        distances = resultDistances[0]['value'];
      });
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(children: [
          Text('Running on: $_platformVersion\n'),
          Text('Today Steps : $steps \n'),
          Text('Today Distances : $distances \n')
        ]),
      ),
    );
  }
}
0
likes
110
pub points
0%
popularity

Publisher

unverified uploader

custom HealthKit reader plugin.

Documentation

API reference

License

BSD-2-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on my_hk_reader