Flutter计步器应用在模拟器及Galaxy S9真机上无法正常获取步数的问题求助
Flutter计步器应用在模拟器及Galaxy S9真机上无法正常获取步数的问题求助
大家好,我是Flutter新手,最近在做一个计步器应用,但不管是iOS模拟器、安卓模拟器,还是我的Galaxy S9真机,都跑不起来,一直报错。我已经尝试了换设备和模拟器,还在AndroidManifest和iOS的Info.plist里加了对应的权限,可问题还是没解决,有没有大佬能帮我看看呀?
我的代码
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的
AndroidManifest.xml和iOS的Info.plist中添加了计步相关的权限
实在搞不懂问题出在哪了,希望有经验的朋友能帮忙分析下,谢谢!
备注:内容来源于stack exchange,提问作者Oussama
相关产品推荐
相关产品推荐

