flutter getstorage example
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
void main() async {
await GetStorage.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage()
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _count = 0;
final datacount = GetStorage();
@override
void initState() {
// TODO: implement initState
super.initState();
if(datacount.read('count')!= null)
{
_count = datacount.read('count');
}
}
@override
Widget build(BuildContext context) {
datacount.writeIfNull("count", 0);
return Scaffold(
body: SafeArea(
child: Center(
child: Container(
child: Text("${datacount.read('count')}"),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState(() {
_count++;
datacount.write("count", _count);
});
},
child: Icon(Icons.add),
),
);
}
}
// https://protocoderspoint.com/flutter-getx-storage-alternative-of-sharedpreferences/