title: Pedometer with Time - Flutter
body: <p>I need to count the steps a user does and the TIME for each step!</p>
  
  <p>I tried to use a pedometer from Flutter but I am facing already some problems. I can't restart the pedometer and the counter seems to be very slow... I mean, I walk several steps, and only after some time the counter updates, I'd like it to be right away, for each step. Another thing is that I need to get the time it took for each step. Do you think I can do it with a pedometer? </p>
  
  <p>Example:
  Step 1 - 1300 milliseconds, 
  Step 2 - 1340 milliseconds, 
  Step 3 - 1240 milliseconds, 
  Step 4 - 1500 milliseconds, 
  Step 5 - 1330 milliseconds, 
  ... </p>
  
  <p>Here's my code:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'dart:async';
  import 'package:pedometer/pedometer.dart';
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatefulWidget {
    @override
    _MyAppState createState() =&gt; new _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
    Pedometer _pedometer;
    StreamSubscription&lt;int&gt; _subscription;
    String _stepCountValue = '?';
  
    @override
    void initState() {
      super.initState();
      initPlatformState();
    }
  
    // Platform messages are asynchronous, so we initialize in an async method.
    Future&lt;void&gt; initPlatformState() async {
      startListening();
    }
  
    void onData(int stepCountValue) {
      print(stepCountValue);
    }
  
    void startListening() {
      setState(() {
        _pedometer = new Pedometer();
        _subscription = _pedometer.pedometerStream.listen(_onData,
            onError: _onError, onDone: _onDone, cancelOnError: true);
      });
    }
  
  // To restart the pedometer, but it doesn't work
    void change(){
      setState(() {
        _pedometer = new Pedometer();
        _stepCountValue = '?';
      },);
    }
  
    void stopListening() {
      _subscription.cancel();
    }
  
    void _onData(int newValue) async {
      print('New step count value: $newValue');
      setState(() =&gt; _stepCountValue = "$newValue");
    }
  
    void _onDone() =&gt; print("Finished pedometer tracking");
  
    void _onError(error) =&gt; print("Flutter Pedometer Error: $error");
  
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
              title: const Text('Pedometer example app'),
            ),
            body: Center(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: &lt;Widget&gt;[
                new Icon(
                  Icons.directions_walk,
                  size: 90,
                ),
                new Text(
                  'Steps taken:',
                  style: TextStyle(fontSize: 30),
                ),
                new Text(
                  '$_stepCountValue',
                  style: TextStyle(fontSize: 100, color: Colors.blue),
                ),
                RaisedButton(
                  child: Text('Restart'),
                  color: Theme.of(context).primaryColor,
                  textColor: Theme.of(context).textTheme.button.color,
                  onPressed: change,
                ),
              ],
            ))),
      );
    }
  }
  </code></pre>