Ensure you init the get_storage
package deal in your void major(){ }
like beneath:
void major() async {
await GetStorage.init();
runApp(MyApp());
}
Right here is the total instance of get_storage :
Code:
import 'package deal:flutter/materials.dart';
import 'package deal:get_storage/get_storage.dart';
void major() async {
await GetStorage.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget construct(BuildContext context) {
return const MaterialApp(
title: 'Flutter App',
house: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({tremendous.key});
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String title="";
String outcome="";
@override
Widget construct(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Textual content('Get Storage Demo'),
),
physique: Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
little one: Column(
mainAxisAlignment: MainAxisAlignment.middle,
youngsters: [
// display result
Text(
result,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your name',
),
onChanged: (value) {
setState(() => name = value);
},
),
ElevatedButton(
onPressed: () {
GetStorage().write('name', name);
// toast
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Saved: $name')),
);
},
child: const Text('Save'),
),
ElevatedButton(
onPressed: () {
setState(() {
result = GetStorage().read('name');
});
},
child: const Text('Display'),
)
],
),
),
);
}
}