Flutter Block Store
Flutter Block Store is a library for storing and retrieving data using the Blockstore service. It provides methods to save, retrieve, and delete data from the blockstore.
Introduction to Block Store
Many users still manage their own credentials when setting up a new Android device. This manual process can become challenging and often results in a poor user experience. The Block Store API, a library powered by Google Play services, looks to solve this by providing a way for apps to save user credentials without the complexity or security risk associated with saving user passwords.
The Block Store API allows your app to store data that it can later retrieve to re-authenticate users on a new device. This helps provide a more seamless experience for the user, as they don't need to see a sign-in screen when launching your app for the first time on the new device.
The benefits of using Block Store include:
- Encrypted credential storage solution for developers. Credentials are end-to-end encrypted when possible.
- Save tokens instead of usernames and passwords.
- Eliminate friction from sign-in flows.
- Save users from the burden of managing complex passwords.
- Google verifies the user's identity.
For more information on Block Store and its features, you can refer to the Google Developers documentation.
Installation
To use the Flutter Block Store library in your Flutter project, follow these steps:
- Add the following dependency to your project's
pubspec.yaml
file:
dependencies:
flutter_block_store: ^0.0.2
- Run the following command to install the dependencies:
flutter pub get
- Import the library in your Dart code:
import 'package:flutter_block_store/flutter_block_store.dart';
Usage
To use the Flutter Block Store library, follow these steps:
-
Import the
flutter_block_store.dart
file:import 'package:flutter_block_store/flutter_block_store.dart';
-
Create an instance of the
FlutterBlockStore
class:FlutterBlockStore blockStore = FlutterBlockStore();
-
Use the available methods to interact with the blockstore:
-
Save Data: Save data to the blockstore.
Future<bool?> save({required String key, required String value})
key
(String): The key associated with the data.value
(String): The data to be saved. It should be a string with a maximum of 4KB per entry.
Returns a
Future<bool>
indicating the success status of the operation. It returnstrue
on success,false
otherwise. -
Retrieve Data: Retrieve data from the blockstore.
Future<String?> retrieve({required String key})
key
(String): The key associated with the data.
Returns a
Future<String>
containing the retrieved data on success, ornull
if no data is found. -
Retrieve All Data: Retrieve all data from the blockstore.
Future<List<dynamic>> retrieveAll()
Returns a
Future<List<dynamic>>
containing a list of all retrieved data on success, or an empty list if no data is found. -
Delete Data: Delete data from the blockstore.
Future<bool?> delete({required String key})
key
(String): The key associated with the data.
Returns a
Future<bool>
indicating the success status of the operation. It returnstrue
on success, or throws an error otherwise. -
Delete All Data: Delete all data from the blockstore.
Future<bool?> deleteAll()
Returns a
Future<bool>
indicating the success status of the operation. It returnstrue
on success, or throws an error otherwise.
-
Example
Here's an example of how to use the Flutter Block Store library:
import 'package:flutter_block_store/flutter_block_store.dart';
void main() async {
FlutterBlockStore blockStore = FlutterBlockStore();
// Save data
await blockStore.save(key: 'username', value: 'John Doe');
// Retrieve data
String? username = await blockStore.retrieve(key: 'username');
print('Username: $username');
// Retrieve all data
List<dynamic> allData = await blockStore.retrieveAll();
print('All Data: $allData');
// Delete data
await blockStore.delete(key: 'username');
// Delete all data
await blockStore.deleteAll();
}
Make sure to handle errors and exceptions that may occur during data operations.