flutter_keystore 0.0.1 copy "flutter_keystore: ^0.0.1" to clipboard
flutter_keystore: ^0.0.1 copied to clipboard

PlatformAndroid

Android KeyStore System for Flutter application

example/lib/main.dart

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

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

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _storeValue = '';
  final _flutterKeystorePlugin = FlutterKeystore();
  final String myDataKey = 'hello';
  final TextEditingController _valueController = TextEditingController();

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

  @override
  void dispose() {
    _valueController.dispose();
    super.dispose();
  }

  Future<void> _loadStoreData() async {
    String value;
    try {
      value = await _flutterKeystorePlugin.read(myDataKey) ?? '(Empty)';
    } on PlatformException {
      value = 'Failed to read store value';
    }

    if (!mounted) return;

    setState(() {
      _storeValue = value;
    });
  }

  Future<void> _onSave() async {
    final String value = _valueController.text.trim();
    if (value.isEmpty) {
      await _onDelete();
    } else {
      await _flutterKeystorePlugin.write(myDataKey, value);
    }
    _valueController.clear();
    _loadStoreData();
  }

  Future<void> _onDelete() async {
    await _flutterKeystorePlugin.delete(myDataKey);
    _loadStoreData();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Expanded(
              child: Center(
                child: Text('Value in keystore: $_storeValue'),
              ),
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 28.0),
                    child: TextField(
                      controller: _valueController,
                      decoration: const InputDecoration(
                        border: OutlineInputBorder(),
                        label: Text('Value in store'),
                      ),
                    ),
                  ),
                  const SizedBox(height: 24),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      ElevatedButton(
                        onPressed: _onSave,
                        child: const Text('Save'),
                      ),
                      const SizedBox(width: 24),
                      ElevatedButton(
                        onPressed: _onDelete,
                        child: const Text('Delete'),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
140
pub points
0%
popularity

Publisher

unverified uploader

Android KeyStore System for Flutter application

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_keystore