Use Plugin in Flutter
Posted By : Pawanpreet Singh | 31-Dec-2018
Flutter provides us the opportunity to create beautiful mobile applications If we want to invoke any functionality of the device we need to add a plugin of it. We will learn by adding Shared Preferences plugin in our application.
Create an application and name it "shared_preferences_demo" then add the following code in its lib/main
import 'package:flutter/material.dart';
void main() => runApp(SharedPreferencesDemo());
class SharedPreferencesDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Shared Preferences Demo',
home: HomePage(title: 'Shared Preferences Demo'),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
TextEditingController textController = new TextEditingController();
String savedData = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
TextField(
controller: textController,
),
FlatButton(
child: Text("Save In Shared Preferences"),
onPressed: () {
saveInSharedPreferences();
},
color: Colors.redAccent,
),
Padding(
padding: EdgeInsets.all(10),
),
Text(savedData),
FlatButton(
child: Text("Get from Shared Preferences"),
onPressed: () {
getFromSharedPreferences();
},
color: Colors.greenAccent,
),
],
));
}
void saveInSharedPreferences() {
debugPrint('save pressed');
}
void getFromSharedPreferences() {
debugPrint('get pressed');
}
}
Finding a plugin:
1. There are various flutter plugins available on the https://pub.dartlang.org/flutter, One can search and find a plugin there easily.
2. Search for shared preferences.
3. Select shared_preferences option.
4. Open Installing tab - Here you will learn how to integrate this plugin in your application.
Integrating Plugin in Our Application
1. Open
2. Add the following in your dependencies.
dependencies:
shared_preferences:
3. Install it via command
flutter packages get
4. Import in your code as the following
import 'package:shared_preferences/shared_preferences.dart';
Add the following code in your lib/main.dart file.
1. Add this in
void saveInSharedPreferences() async {
String dataToSave = textController.text;
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('data', dataToSave);
textController.text = '';
}
2. Add this in
void getFromSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
savedData = await prefs.getString('data');
debugPrint(savedData);
}
Run the app.
Now you will be able to save and get from shared preferences in your application.
Hope you enjoyed my post
References:
1. https://pub.dartlang.org/
2. https://github.com/flutter/plugins
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pawanpreet Singh
Pawanpreet is an seasoned Project Manager with a wealth of knowledge in software development, specializing in frontend and mobile applications. He possesses a strong command of project management tools, including Jira, Trello, and others. With a proven track record, he has successfully overseen the delivery of multiple software development projects, managing budgets and large teams. Notable projects he has contributed to include TimeForge, Yogyata, Kairos, Veto, Inspirien App, and more. Pawanpreet excels in developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within allocated resources. He collaborates closely with clients to define project scope and requirements, establish timelines and milestones, and effectively manage expectations. Regular project status meetings are conducted by him, providing clients and stakeholders with consistent updates on project progress, risks, and issues. Additionally, he coaches and mentors project leads, offering guidance on project management best practices and supporting their professional development.