How to Get Battery Level, Charging Status in Flutter

In this guide, we are going to show you the easiest way to get Battery Information such as battery level percentage, charging status, battery health, plugged status, and many more in Android and iOS. See the example below for deeper insight. 

First, you need to add battery_info Flutter package in your dependency by adding the following lines in pubspec.yaml file. 

dependencies:
  flutter:
    sdk: flutter
  battery_info: ^1.0.7

This Package can get the following information from Android and iOS:

Android iOS
  • Battery Level
  • Charging Status
  • Charge Time Remaining
  • Current Average
  • Current Now
  • Health
  • Plugged Status
  • Battery Presence
  • Scale
  • Remaining Energy
  • Technology
  • Temperature
  • Voltage
  • Battery Level
  • Charging Status

Import package file in your script. 

import 'package:battery_info/model/android_battery_info.dart';
//import for android
import 'package:battery_info/model/iso_battery_info.dart';
//import for iOS

int level = AndroidBatteryInfo().batteryLevel;

int level = IosBatteryInfo().batteryLevel;

You can retrieve more information like charging status, plugged status, health, and many more in Android using Flutter. See the complete example below for more understanding. 

import 'package:battery_info/battery_info_plugin.dart';
import 'package:battery_info/model/android_battery_info.dart';
//import for android
import 'package:battery_info/model/iso_battery_info.dart';
//import for iOS
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {

  String batterylevel = "", 
         batteryhealth = "",
         chargingstatus = "",
         pluggedstatus = "";

  @override
  void initState() {
    
     AndroidBatteryInfo infoandroid = AndroidBatteryInfo();
     //IosBatteryInfo infoios = IosBatteryInfo(); use for iOS
     //only battery level and charging status are retrived from iOS
     
     Future.delayed(Duration.zero, () async { //there is async (await) execution inside it
        infoandroid = await BatteryInfoPlugin().androidBatteryInfo;
        // infoios = await BatteryInfoPlugin().iosBatteryInfo;  for iOS
        batterylevel = infoandroid.batteryLevel.toString();
        batteryhealth = infoandroid.health.toString();
        chargingstatus = infoandroid.chargingStatus.toString();
        pluggedstatus = infoandroid.pluggedStatus.toString();

        setState(() {
          //refresh UI
        });
     });

     BatteryInfoPlugin().androidBatteryInfoStream.listen((AndroidBatteryInfo batteryInfo) { 
     //add listiner to update values if there is changes
          infoandroid = batteryInfo;
          batterylevel = infoandroid.batteryLevel.toString();
          batteryhealth = infoandroid.health.toString();
          chargingstatus = infoandroid.chargingStatus.toString();
          pluggedstatus = infoandroid.pluggedStatus.toString();

          setState(() {
            //refresh UI
          });
     });

    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Test App",
        home: Scaffold( 
            appBar: AppBar( 
              title: Text("Battery Info"),
              backgroundColor: Colors.redAccent,
            ),
            
            body: Container( 
              alignment: Alignment.center,
              padding: EdgeInsets.all(20),
              child: Column( 
                children: [
                  Text("Battery Level: $batterylevel %"),
                  Text("Battery Health: $batteryhealth"),
                  Text("Charging Status: $chargingstatus"), 
                  //values: ChargingStatus.charging, discharging, full, unkown
                  //
                  Text("Plugged Status : $pluggedstatus"), 
                ],
              )
            )
        ),
    );
  }
}

In this way, you can get Battery Information in Android and iOS using Flutter. 

1 Commet on this Article

Pinsky Pirate

Hi,
I was just using this package on my flutter program.

But, when I just copy pasted the same code given on this site. The values of the battery health and all came as ’null’. It was because maybe I was using Android Emulator, but idk.

Please help me out

 

3 years ago


Please Wait...