Skip to content

Instantly share code, notes, and snippets.

@RipplesCode
Created April 8, 2021 15:24
Show Gist options
  • Save RipplesCode/ca49986b0f0bfe146b04f90c82fed580 to your computer and use it in GitHub Desktop.
Save RipplesCode/ca49986b0f0bfe146b04f90c82fed580 to your computer and use it in GitHub Desktop.
GetStorage & Email Validation
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
Future<void> main() async {
await GetStorage.init(); // Initialize storage driver
runApp(MyApp());
}
class MyApp extends StatelessWidget {
var emailEditingController = TextEditingController();
var storage = GetStorage();
@override
Widget build(BuildContext context) {
// TODO: implement build
return GetMaterialApp(
title: "GetStorage & Email Validation",
home: Scaffold(
appBar: AppBar(title: Text("GetStorage & Email Validation")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(16.0),
child: TextField(
controller: emailEditingController,
)),
SizedBox(
height: 8,
),
Padding(
padding: EdgeInsets.all(16.0),
child: RaisedButton(
child: Text("Write"),
onPressed: () {
if (GetUtils.isEmail(emailEditingController.text)) {
storage.write("email",
emailEditingController.text);
} else {
Get.snackbar(
"InCorrect Email", "Provide Email in valid format",
colorText: Colors.white,
backgroundColor: Colors.red,
snackPosition: SnackPosition.BOTTOM);
}
},
),
),
SizedBox(
height: 8,
),
RaisedButton(
child: Text('Read'),
onPressed: (){
print("The Email is ${storage.read("email")}");
},
)
],
),
),
),
);
}
}
/* It is used for persistent key/value storage
Can store String, int, double, Map and List
// To listen for changes
// var listen= storage.listen(() {print("Email Changed");});
//when subscribed to listen event it should be disposed by using
// storage.removeListen(listen);
//To listen for changes in key
// storage.listenKey('email', (value){
// print('new key is $value');
// });
//Remove a key
storage.remove('email);
//erase the container
storage.erase();
//create container with a name
GetStorage g = GetStorage('MyStorage');
await GetStorage.init('MyStorage'); // initialize
* */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment