For this recipe, we will create an app that takes a String from a text field and stores it securely using the flutter_secure_storage library. Follow these steps:
- Edit the pubspec.yaml file by adding the flutter_secure_storage dependency. You'll find the latest version at https://pub.dev/packages/flutter_secure_storage:
flutter_secure_storage: ^4.1.0
- In the main.dart file, copy the code available at https://bit.ly/flutter_secure_storage.
- At the top of the main.dart file, add the required import:
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
- At the top of the _myHomePageState class, create secure storage:
final storage = FlutterSecureStorage();
final myKey = 'myPass';
- In the _myHomePageState class, add the method for writing data to the secure storage:
Future writeToSecureStorage() async {
await storage.write(key: myKey, value: pwdController.text);
}
- In the build() method of the _myHomePageState class...