In Flutter, packages and plugins are pre-built pieces of code that you can reuse in your app to add new features quickly, without writing everything from scratch.
1. Open your pubspec.yaml file in your Flutter project.
2. Under dependencies:, add the package name and version.
dependencies:
flutter:
sdk: flutter
http: ^0.13.5
3. Run flutter pub get in the terminal or your IDE will fetch and install the package.
4. Import and use it in your Dart code:
import 'package:http/http.dart' as http;
void fetchData() async {
final response = await http.get(Uri.parse('https://example.com'));
print(response.body);
}
Think of packages and plugins like tools in a toolbox. If you want to hang a picture (add a feature), you can use a hammer or drill (package/plugin) someone else made, instead of building those tools yourself.
shared_preferences Plugin
// Add to pubspec.yaml
dependencies:
shared_preferences: ^2.0.15
// Import and use in your code
import 'package:shared_preferences/shared_preferences.dart';
void saveData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('username', 'flutterUser');
print('Data saved!');
}
void loadData() async {
final prefs = await SharedPreferences.getInstance();
String? username = prefs.getString('username');
print('Loaded username: \$username');
}
Shared Preferences plugin allows storing simple data locally on the device like a tiny database.
For more details, see Flutter's official docs on packages & plugins.