Module 7
Module 7
1. Accelerometer
This listens to the device's acceleration along the x, y, and z axes, including gravity.
import 'package:sensors_plus/sensors_plus.dart';
void listenToAccelerometer() {
accelerometerEvents.listen(
(AccelerometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case the sensor is not available
},
cancelOnError: true,
);
}
// Output Example: [AccelerometerEvent (x: 0.0, y: 9.8, z: 0.0)]
Error Handling
onError:
o Handles scenarios where the sensor might not be available (e.g., on certain
devices or platforms).
cancelOnError:
o Ensures the listener stops listening to the stream when an error occurs.
2. User Accelerometer
This listens to the device's acceleration along the x, y, and z axes, excluding gravity.
import 'package:sensors_plus/sensors_plus.dart';
void listenToUserAccelerometer() {
userAccelerometerEvents.listen(
(UserAccelerometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case the sensor is not available
},
cancelOnError: true,
);
}
// Output Example: [UserAccelerometerEvent (x: 0.0, y: 0.0, z: 0.0)]
3. Gyroscope
This listens to the device's rotation rate around the x, y, and z axes.
import 'package:sensors_plus/sensors_plus.dart';
void listenToGyroscope() {
gyroscopeEvents.listen(
(GyroscopeEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case the sensor is not available
},
cancelOnError: true,
);
}
// Output Example: [GyroscopeEvent (x: 0.0, y: 0.0, z: 0.0)]
4. Magnetometer
This listens to the device's magnetic field strength along the x, y, and z axes.
import 'package:sensors_plus/sensors_plus.dart';
void listenToMagnetometer() {
magnetometerEvents.listen(
(MagnetometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case the sensor is not available
},
cancelOnError: true,
);
}
// Output Example: [MagnetometerEvent (x: -23.6, y: 6.2, z: -34.9)]
5. Barometer
void listenToBarometer() {
barometerEvents.listen(
(BarometerEvent event) {
print(event);
},
onError: (error) {
// Logic to handle error
// Needed for Android in case the sensor is not available
},
cancelOnError: true,
);
}
// Output Example: [BarometerEvent (pressure: 1000.0)]
Usage
You can call the specific functions in your Flutter app depending on which sensor data
you want to access:
void main() {
listenToAccelerometer();
listenToUserAccelerometer();
listenToGyroscope();
listenToMagnetometer();
listenToBarometer();
}
Each function is modular and handles a single sensor, making the code easy to maintain
and debug.
Sensor Calibration Example
import 'package:sensors_plus/sensors_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SensorCalibration {
double? accelerometerOffsetX;
double? accelerometerOffsetY;
double? accelerometerOffsetZ;
accelerometerEvents.listen(
(AccelerometerEvent event) {
accelXReadings.add(event.x);
accelYReadings.add(event.y);
accelZReadings.add(event.z);
_saveCalibrationData();
},
onError: (error) => print(error), // Handle errors in case
the sensor is not available
);
prefs.setDouble('accelerometerOffsetX',
accelerometerOffsetX!);
prefs.setDouble('accelerometerOffsetY',
accelerometerOffsetY!);
prefs.setDouble('accelerometerOffsetZ',
accelerometerOffsetZ!);
Future<AccelerometerEvent> getCalibratedAccelerometerData()
async {
return accelerometerEvents.map((event) {
return AccelerometerEvent(
);
}
Location Code
import 'package:location/location.dart';
if (!_serviceEnabled) {
if (!_serviceEnabled) {
return;
}
// Step 2: Check if the app has location permissions
if (_permissionGranted == PermissionStatus.denied) {
if (_permissionGranted != PermissionStatus.granted) {
return;
LocationPermission isPermissionGranted;
if(!isServiceEnabled){
if(isPermissionGranted == LocationPermission.denied){
while(isPermissionGranted == LocationPermission.denied){
if(isPermissionGranted == LocationPermission.deniedForever){
// Handle permission
// return Geolocator.getCurrentPosition();
return Geolocator.getCurrentPosition(desiredAccuracy:
LocationAccuracy.high);