enviro_sensors 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

enviro_sensors #


A plugin that enables calling native device enviroment sensors. Readings are sent over an EventChannel and can be accessed with a listener to a Dart Stream.

Currently, only android enviroment sensors streams are implemented.

Getting Started #


Install the package. Add import (i.e. import 'package:enviro_sensors/enviro_sensors.dart';) Instantiate an appropriate stream (baromterEvents, lightmeterEvents, ambientTempEvents, humidityEvents) Readings are received as doubles in their corresponding measurement units, and can be accessed in the ".reading" property of each event. (see Example/lib/main.dart for more).

TODO: #


  • improve the interface for barometerEvents stream, add way to change SENSOR_DELAY if the device enables it.
  • add barometerEvents stream on iOS.
  • write docs and improve "example".

0.0.1 #

Android: #

  • only barometer stream is implemented.

iOS: #

  • not implemented yet.

1.0.0 #

Android: #

  • added light sensor, ambient temp sensor and humidity sensor streams.

iOS: #

  • not implemented yet.

1.0.1 #

  • commented out a code block that caused a bug in EnviroSensorsPlugin.java

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:enviro_sensors/enviro_sensors.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // double _barometerReading;
  Stream<BarometerEvent> _pressureStream;
  Stream<LightmeterEvent> _lightmeterStream;
  Stream<AmbientTempEvent> _ambientTempStream;
  Stream<HumidityEvent> _humidityStream;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // double barometerReading;
    Stream<BarometerEvent> pressureStream;
    Stream<LightmeterEvent> lightmeterStream;
    Stream<AmbientTempEvent> ambientTempStream;
    Stream<HumidityEvent> humidityStream;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      pressureStream = barometerEvents.asBroadcastStream();
      lightmeterStream = lightmeterEvents.asBroadcastStream();
      ambientTempStream = ambientTempEvents.asBroadcastStream();
      humidityStream = humidityEvents.asBroadcastStream();
      // barometerReading = await EnviroSensors.barometerReading;
    } on PlatformException {
      // barometerReading = null;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
    _pressureStream = pressureStream;
    _lightmeterStream = lightmeterStream;
    _ambientTempStream = ambientTempStream;
    _humidityStream = humidityStream;
    setState(() {
      // _barometerReading = barometerReading;
    });
  }

  String _addUnitOfMeasurementSuffix(double prefix, String suffix) {
    return prefix.toStringAsFixed(1) + ' ' + suffix;
  }
  // Future<double> dispatchBarometerCall() async {
  //   double barometerReading;
  //   try {
  //     barometerReading = await EnviroSensors.barometerReading;
  //   } on PlatformException {
  //     barometerReading = null;
  //   }
  //   return barometerReading;
  // }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Center(child: const Text('Enviro_Sensors')),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Divider(
              color: Colors.black,
            ),
            SizedBox(
              height: 35,
            ),
            // Barometer (stream and mathod-call button):
            Text('Barometer:'),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                StreamBuilder(
                    stream: _pressureStream,
                    builder: (BuildContext context, dynamic snapshot) {
                      if (snapshot.connectionState == ConnectionState.active) {
                        return Text((snapshot.data.reading == null)
                            ? 'null'
                            : '${snapshot.data.reading.toStringAsFixed(1)} hPa');
                      }
                      return Text('Baromter stream value');
                    }),
                FlatButton(
                  child: Text('disabled'),
                  onPressed: null,
                  color: Colors.purpleAccent,
                ),
              ],
            ),
            Divider(
              color: Colors.black,
            ),
            SizedBox(
              height: 35,
            ),
            // Light sensor (stream and mathod-call button):
            Text('Light sensor:'),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                StreamBuilder(
                    stream: _lightmeterStream,
                    builder: (BuildContext context, dynamic snapshot) {
                      if (snapshot.connectionState == ConnectionState.active) {
                        return Text((snapshot.data.reading == null)
                            ? 'null'
                            : '${snapshot.data.reading.toStringAsFixed(1)} lx');
                      }
                      return Text('Light sensor stream value');
                    }),
                FlatButton(
                  child: Text('disabled'),
                  onPressed: null,
                  color: Colors.purpleAccent,
                ),
              ],
            ),
            Divider(
              color: Colors.black,
            ),
            SizedBox(
              height: 35,
            ),
            // Ambient temp sensor (stream and mathod-call button):
            Text('Ambient temperature sensor:'),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                StreamBuilder(
                    stream: _ambientTempStream,
                    builder: (BuildContext context, dynamic snapshot) {
                      if (snapshot.connectionState == ConnectionState.active) {
                        return Text((snapshot.data.reading == null)
                            ? 'null'
                            : '${snapshot.data.reading.toStringAsFixed(1)} °C');
                      }
                      return Text('Ambient temp stream value');
                    }),
                FlatButton(
                  child: Text('disabled'),
                  onPressed: null,
                  color: Colors.purpleAccent,
                ),
              ],
            ),
            Divider(
              color: Colors.black,
            ),
            SizedBox(
              height: 35,
            ),
            // Humidity sensor (stream and mathod-call button):
            Text('Humidity sensor:'),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                StreamBuilder(
                    stream: _humidityStream,
                    builder: (BuildContext context, dynamic snapshot) {
                      if (snapshot.connectionState == ConnectionState.active) {
                        return Text((snapshot.data.reading == null)
                            ? 'null'
                            : '${snapshot.data.reading.toStringAsFixed(1)} %');
                      }
                      return Text('Humidity stream value');
                    }),
                FlatButton(
                  child: Text('disabled'),
                  onPressed: null,
                  color: Colors.purpleAccent,
                ),
              ],
            ),
            Divider(
              color: Colors.black,
            ),
            SizedBox(
              height: 35,
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  enviro_sensors: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:enviro_sensors/enviro_sensors.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Linux

Because:

  • package:enviro_sensors/enviro_sensors.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Web

Because:

  • package:enviro_sensors/enviro_sensors.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Windows

Because:

  • package:enviro_sensors/enviro_sensors.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform macOS

Because:

  • package:enviro_sensors/enviro_sensors.dart that declares support for platforms: Android, iOS

Package not compatible with SDK dart

Because:

  • enviro_sensors that is a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

19 out of 19 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/enviro_method_calls.dart.

Run flutter format to format lib/enviro_method_calls.dart.

Format lib/enviro_sensors.dart.

Run flutter format to format lib/enviro_sensors.dart.

Format lib/enviro_streams.dart.

Run flutter format to format lib/enviro_streams.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test