Hey i'm beginner to flutter and i'm trying to create a step counter app but i get this error on iOS and android emulators and on my physical device ( galaxy s9 ) as well.
Here's my code:
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,
),
),
],
),
],
),
],
),
),
);
}
}
and here's the error i'm getting:
D/SensorManager( 8584): registerListener fail (1) :: 1, step_counter Non-wakeup, 0, 0,
I tried other devices and other emulators but same issue i'm not sure what is the problem. i also added all the permissions on both manifest and Info for both android and iOS if anyone can
Make sure that your app has the necessary permissions to access the step counter, e.g. via the PermissionHandler package
Only then, call