native_encrypted_storage 0.1.0
native_encrypted_storage: ^0.1.0 copied to clipboard
Secure storage with native encryption using Android Keystore. All encryption happens in native code for maximum security. iOS support coming soon.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:native_encrypted_storage/native_encrypted_storage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Native Encrypted Storage Demo',
theme: ThemeData(primarySwatch: Colors.red),
home: const DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({super.key});
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
final storage = NativeEncryptedStorage.getInstance;
final _keyController = TextEditingController(text: 'your_key');
final _valueController = TextEditingController(text: '1234Token');
String _outPut = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Encryption Test'), centerTitle: true),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
TextField(
controller: _keyController,
decoration: InputDecoration(
labelText: 'Key',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _valueController,
decoration: InputDecoration(
labelText: 'Value',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _getData,
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
child: Text(
'Get Value',
style: const TextStyle(color: Colors.white),
),
),
SizedBox(height: 10),
ElevatedButton(
onPressed: _saveData,
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
child: Text(
'Save Value',
style: const TextStyle(color: Colors.white),
),
),
SizedBox(height: 10),
SizedBox(height: 24),
if (_outPut.isNotEmpty) ...[
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(8),
),
child: SelectableText(
_outPut,
style: const TextStyle(fontSize: 14),
),
),
],
],
),
),
),
);
}
Future<void> _saveData() async {
try {
final key = _keyController.text;
final value = _valueController.text;
setState(() => _outPut = 'Saveing Loading');
await storage.write(key: key, value: value);
setState(() {
_outPut =
'Saveing Successfully\n\n'
'Value:$value\n'
'Key : $key\n\n';
});
} catch (e) {
setState(() => _outPut = ' Error: $e');
}
}
Future<void> _getData() async {
try {
final key = _keyController.text;
setState(() => _outPut = 'Getting Loading');
final result = await storage.read(key: key);
if (result == null) {
setState(() => _outPut = 'Key is not valid');
return;
}
setState(() {
_outPut =
'Getting Successfully\n\n'
'Result:$result\n'
'Key : $key\n\n';
});
} catch (e) {
setState(() => _outPut = ' Error: $e');
}
}
}