dart 为什么StepCount传感器在模拟器和物理设备上都不起作用?

j2cgzkjk  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(108)

嘿,我是初学者Flutter,我试图创建一个步计数器应用程序,但我得到这个错误的iOS和Android模拟器和我的物理设备(Galaxy S9)以及。
下面是我的代码:

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

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late String _timeString;
  int initSteps = 0;
  int initKcals = 0;  
  double initMiles = 0.0;
  int initMinutes = 0;
  double progressValue = 0.0;

  StreamSubscription<StepCount>? _subscription;

  @override
  void initState() {
    super.initState();
    _listenToSteps();
    _timeString = _formatDateTime(DateTime.now());
    Timer.periodic(const Duration(seconds: 1), (Timer t) => _getTime());
  }

  String _formatDateTime(DateTime dateTime) {
    return '${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
  }

  void _getTime() {
    final DateTime now = DateTime.now();
    final String formattedDateTime = _formatDateTime(now);
    setState(() {
      _timeString = formattedDateTime;
    });
  }

  void _listenToSteps() {
    _subscription = Pedometer.stepCountStream.listen(
      _onStepCount,
      onError: _onError,
      onDone: _onDone,
      cancelOnError: true,
    );
  }

  void _onStepCount(StepCount event) {
    setState(() {
      initSteps = event.steps;
      progressValue = event.steps / 10000;
      print("Step Count: ${event.steps}");
    });
  }

  void _onDone() {} // Handle when stream is done if needed

  void _onError(error) {
    print("An error occurred while fetching step count: $error");
  }

  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.black,
        body: Column(
          children: [
            Container(
              padding: const EdgeInsets.fromLTRB(25.0, 65, 25.0, 0.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text('iCounter',
                      style: TextStyle(
                          color: Colors.green,
                          fontSize: 30,
                          fontWeight: FontWeight.bold)),
                  Text(_timeString,
                      style: const TextStyle(
                          color: Colors.white,
                          fontSize: 20,
                          fontWeight: FontWeight.bold)),
                ],
              ),
            ),
            const SizedBox(height: 25.0),
            LinearProgressIndicator(
              value: progressValue, // progress
              backgroundColor: Colors.grey, // Color of the track
              valueColor: const AlwaysStoppedAnimation<Color>(
                  Colors.green), // Color of the progress bar
            ),
            const SizedBox(height: 30.0),
            const Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.keyboard_arrow_left,
                  color: Colors.grey,
                  size: 60.0,
                ),
                SizedBox(width: 30.0),
                Text(
                  'Today',
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 35.0,
                  ),
                ),
                SizedBox(width: 30.0),
                Icon(
                  Icons.keyboard_arrow_right,
                  color: Colors.grey,
                  size: 60.0,
                ),
              ],
            ),
            const SizedBox(height: 48),
            Center(
              child: Text(
                '$initSteps',
                style: const TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 100.0,
                ),
              ),
            ),
            const Text(
              'steps today',
              style: TextStyle(
                color: Colors.grey,
                fontSize: 20.0,
              ),
            ),
            const SizedBox(height: 30.0),
            Text(
              '$initSteps/10.000s',
              style: const TextStyle(
                color: Colors.green,
                fontSize: 25.0,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 40.0),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Column(
                  children: [
                    Text(
                      '$initKcals',
                      style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    const SizedBox(height: 5.0),
                    const Text(
                      'KCAL',
                      style: TextStyle(
                        color: Colors.grey,
                      ),
                    ),
                  ],
                ),
                const SizedBox(width: 50.0),
                Column(
                  children: [
                    Text(
                      '$initMiles',
                      style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    const SizedBox(height: 5.0),
                    const Text(
                      'MILES',
                      style: TextStyle(
                        color: Colors.grey,
                      ),
                    ),
                  ],
                ),
                const SizedBox(width: 50.0),
                Column(
                  children: [
                    Text(
                      '$initMinutes',
                      style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    const SizedBox(height: 5.0),
                    const Text(
                      'Minutes',
                      style: TextStyle(
                        color: Colors.grey,
                      ),
                    ),
                  ],
                ),
              ],
            ),
            const SizedBox(height: 40.0),
            const Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'MON',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'TUE',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'WED',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'THU',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'FRI',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'SAT',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(width: 8.0),
                  Column(
                    children: [
                      Icon(
                        Icons.circle_outlined,
                        color: Colors.white,
                        size: 50.0,
                      ),
                      SizedBox(width: 8.0),
                      Text(
                        'SUN',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
          ],
        ),
      ),
    );
  }
}

这是我得到的错误:

D/SensorManager( 8584): registerListener fail (1) :: 1, step_counter  Non-wakeup, 0, 0,

我试过其他设备和其他模拟器,但同样的问题,我不知道是什么问题.我还添加了所有权限的清单和信息为Android和iOS的,如果有人可以

ig9co6j1

ig9co6j11#

确保您的应用具有访问步数计数器的必要权限,例如:通过许可证包

await Permission.activityRecognition.request()

只有到那时,

Pedometer.stepCountStream.listen(...)

相关问题