secure_store_plugin 0.0.3 copy "secure_store_plugin: ^0.0.3" to clipboard
secure_store_plugin: ^0.0.3 copied to clipboard

This is a plugin for storing data with high security.

example/lib/main.dart

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

import 'package:secure_store_plugin/secure_store_plugin.dart';

class User {
  final int id;
  final String name;
  final int age;
  final String email;

  const User({
    required this.id,
    required this.name,
    required this.age,
    required this.email,
  });

  factory User.fromJson(Map<String, dynamic> json) => User(
        id: json['id'],
        name: json['name'],
        age: json['age'],
        email: json['email'],
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'age': age,
        'email': email,
      };
}

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

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

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

class _MyAppState extends State<MyApp> {
  int? _int;
  String? _text;
  bool? _bool;
  User? _user;
  List<User> _list = [];

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Security store example'),
        ),
        body: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
            child: Column(children: [
              Text('Int: $_int'),
              const SizedBox(height: 24),
              Text('String: $_text'),
              const SizedBox(height: 24),
              Text('Bool: $_bool'),
              const SizedBox(height: 24),
              Text('Object: ${_user?.toJson().toString() ?? ''}'),
              const SizedBox(height: 24),
              Text('List: ${_list.map((e) => e.toJson()).toString()}'),
              const SizedBox(height: 24),
              SizedBox(width: double.infinity,
                child: IconButton(onPressed: () async {
                  try {
                    _int = await SecureStorePlugin.read<int>('age');
                    _bool = await SecureStorePlugin.read<bool>('premium');
                    _text = await SecureStorePlugin.read<String>('token');
                    final userJson = await SecureStorePlugin.read<Map<String, dynamic>>('user');
                    if (userJson != null) {
                      _user = User.fromJson(userJson);
                    }
                    final listJson = await SecureStorePlugin.read<List<dynamic>>('users');
                    if (listJson != null) {
                      _list = listJson
                      .map((e) => User.fromJson(Map<String, dynamic>.from(e)))
                      .toList();
                    }
                    setState(() {
                      
                    });
                  } catch(e) {
                    debugPrint('[error] refresh: $e');
                  }
                }, icon: Text('Refresh')),
              ),
            ],),
          ),
        ),
        bottomNavigationBar: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,
        spacing: 16,
        children: [
          IconButton(onPressed: _saveInt, icon: Text('Int')),
        IconButton(onPressed: _saveString, icon: Text('String')),
        IconButton(onPressed: _saveBool, icon: Text('Bool')),
        IconButton(onPressed: _saveObject, icon: Text('Object')),
        IconButton(onPressed: _saveList, icon: Text('List')),
        ],),
      ),
    );
  }

  String getRandomString({int length = 12}) {
    const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    final rand = Random.secure();
    return List.generate(length, (index) => chars[rand.nextInt(chars.length)]).join();
  }

  Future<void> _saveInt() async {
    try {
      await SecureStorePlugin.write("age", secureRandomInt(20, 100));
    } catch(e) {
      debugPrint('[error] _saveInt: $e');
    }
  }

  Future<void> _saveBool() async {
    try{
      await SecureStorePlugin.write("premium", true);
    } catch(e) {
      debugPrint('[error] _saveBool: $e');
    }
  }

  Future<void> _saveString() async {
    try{
      await SecureStorePlugin.write("token", getRandomString());
    } catch(e) {
      debugPrint('[error] _saveString: $e');
    }
  }

  Future<void> _saveObject() async {
    try{
      await SecureStorePlugin.write("user", User(id: 1, name: 'Hung Nguyen', age:  33,email:  '[email protected]').toJson());
    } catch(e) {
      debugPrint('[error] _saveObject: $e');
    }
  }

  Future<void> _saveList() async {
    try{
      await SecureStorePlugin.write("users", [
        User(id: 1, name: 'Hung Nguyen', age:  secureRandomInt(20, 70),email:  '[email protected]').toJson(),
        User(id: 2, name: 'Hung Nguyen', age:  secureRandomInt(20, 70),email:  '[email protected]').toJson(),
        User(id: 3, name: 'Hung Nguyen', age:  secureRandomInt(20, 70),email:  '[email protected]').toJson(),
        User(id: 4, name: 'Hung Nguyen', age:  secureRandomInt(20, 70),email:  '[email protected]').toJson(),
        User(id: 5, name: 'Hung Nguyen', age:  secureRandomInt(20, 70),email:  '[email protected]').toJson(),
      ]);
    } catch(e) {
      debugPrint('[error] _saveList: $e');
    }
  }

  int secureRandomInt(int min, int max) {
    final random = Random.secure();
    return min + random.nextInt(max - min + 1);
  }
}
7
likes
130
points
8
downloads

Publisher

verified publisherit36vn.com

Weekly Downloads

This is a plugin for storing data with high security.

Documentation

API reference

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on secure_store_plugin

Packages that implement secure_store_plugin