ios_physical_memory 0.0.6 ios_physical_memory: ^0.0.6 copied to clipboard
A new Flutter plugin to retrieve device's Physical Memory and current available Memory (This plugin is for IOS Only). Written in Obj-C.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ios_physical_memory/ios_physical_memory.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _physicalMemory = 'Unknown';
String _availableMemory = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
initPhysicalMemory();
initAvailableMemory();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await IosPhysicalMemory.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 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;
setState(() {
_platformVersion = platformVersion;
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPhysicalMemory() async {
String physicalMemory;
try {
physicalMemory = await IosPhysicalMemory.physicalMemory ?? 'Unknown platform version';
} on PlatformException {
physicalMemory = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_physicalMemory = physicalMemory;
});
}
Future<void> initAvailableMemory() async {
String availableMemory;
try {
availableMemory = await IosPhysicalMemory.availableFreeMemory ?? 'Unknown platform version';
} on PlatformException {
availableMemory = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_availableMemory = availableMemory;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
Text('Running on: $_platformVersion\n'),
Text('Total Memory: $_physicalMemory\n'),
Text('Available Memory: $_availableMemory\n'),
],
),
),
),
);
}
}