memory_info 0.1.0
memory_info: ^0.1.0 copied to clipboard
Flutter package to get device memory info(ram&rom) at android/ios devices
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:memory_info/memory_info.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Memory? _memory;
DiskSpace? _diskSpace;
final List<String> _pressureEvents = [];
StreamSubscription<MemoryPressureEvent>? _pressureSubscription;
@override
void initState() {
super.initState();
getMemoryInfo();
_pressureSubscription = MemoryInfoPlugin().onMemoryPressure.listen((event) {
setState(() {
_pressureEvents.insert(
0,
'${DateTime.now().toIso8601String().substring(11, 19)} '
'${event.level.name}'
'${event.androidTrimLevel != null ? ' (trim ${event.androidTrimLevel})' : ''}',
);
});
getMemoryInfo();
});
}
@override
void dispose() {
_pressureSubscription?.cancel();
super.dispose();
}
Future<void> getMemoryInfo() async {
Memory? memory;
DiskSpace? diskSpace;
try {
memory = await MemoryInfoPlugin().memoryInfo;
diskSpace = await MemoryInfoPlugin().diskSpace;
} on PlatformException catch (e) {
debugPrint('error $e');
}
if (!mounted) return;
setState(() {
_memory = memory;
_diskSpace = diskSpace;
});
}
@override
Widget build(BuildContext context) {
final encoder = JsonEncoder.withIndent(' ');
final memInfo = encoder.convert(_memory?.toMap());
final diskInfo = encoder.convert(
_diskSpace == null
? null
: {..._diskSpace!.toMap(), 'diskUsedSpace': _diskSpace!.usedSpace},
);
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: getMemoryInfo,
child: Icon(Icons.update),
),
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: ListView(
padding: const EdgeInsets.all(8.0),
children: [
Text('MemInfo:\n', style: TextStyle(fontWeight: FontWeight.w800)),
Text(memInfo),
Text('\n--------------------------------------------\n'),
Text('DiskInfo:\n', style: TextStyle(fontWeight: FontWeight.w800)),
Text(diskInfo),
Text('\n--------------------------------------------\n'),
Text('Memory pressure:\n',
style: TextStyle(fontWeight: FontWeight.w800)),
Text(_pressureEvents.isEmpty
? 'No events'
: _pressureEvents.join('\n')),
],
),
),
);
}
}