Unlock Flutter’s True Potential

Welcome to Day 13 of our Flutter journey! Today we’re diving into one of Flutter’s most powerful features – its package ecosystem. With over 25,000 packages available, you can add incredible functionality to your apps without writing everything from scratch.

What Are Pub Packages?

Pub packages are reusable code libraries that solve common problems so you don’t have to reinvent the wheel. They come in three main flavors:

Plugins

Access native device features like camera, GPS, sensors, and more through platform-specific code.

Platform-specific
Bridge to native APIs

Dart Packages

Pure Dart code that adds utilities, helpers, or implements complex algorithms.

Pure Dart
Utilities

UI Kits

Pre-designed widgets, screens, and components to accelerate UI development.

Design elements
Custom widgets

Finding Packages on pub.dev

The official package repository for Dart and Flutter apps is pub.dev. Here’s how to evaluate packages effectively:

Check Health Metrics

100+ Likes
Null Safety
< 80% Health
Unresolved Issues

Evaluate Maintenance

Look for packages that have been updated in the last 6 months. Check the “Last Updated” date and review the changelog to see how actively maintained it is.

Review Documentation

Good packages have clear documentation with usage examples. Check the README and example folder to see how easy it is to integrate.

Adding Packages to Your Project

Adding packages to your Flutter project is a straightforward process. Here’s how to do it:

Step 1: Add dependency to pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
      
  # Add your packages here
  url_launcher: ^7.0.0   # Open browser URLs
  shared_preferences: ^2.2.0 # Local storage
  http: ^0.13.5          # HTTP requests
Step 2: Install packages
# Run in your terminal:
flutter pub get
Step 3: Import in your Dart file
import 'package:url_launcher/url_launcher.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;

Practical Examples

Example 1: url_launcher

Open web URLs, send emails, make phone calls, and more with this versatile package.

Launching a URL
void _launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

// Usage in a button:
ElevatedButton(
  onPressed: _launchURL,
  child: Text('Visit Flutter Website'),
)

Example 2: shared_preferences

Store simple data persistently on the user’s device with this key-value storage solution.

Storing and Retrieving Data
// Save data
void _saveData() async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setInt('counter', 42);
  await prefs.setString('username', 'flutter_dev');
}

// Read data
void _readData() async {
  final prefs = await SharedPreferences.getInstance();
  final counter = prefs.getInt('counter') ?? 0;
  final username = prefs.getString('username') ?? 'Guest';
  
  print('Counter: $counter, Username: $username');
}

// Remove data
void _removeData() async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.remove('counter');
}

Your Challenge

Create a settings screen that:

  • Stores the user’s name and theme preference using shared_preferences
  • Has a button that launches your GitHub profile using url_launcher
  • Includes a switch for dark mode that persists between app launches

This will help you practice both packages together!

Best Practices

  • Version constraints: Use caret syntax (^7.0.0) for safe updates
  • Audit regularly: Run flutter pub outdated to find updates
  • Check licenses: Especially important for commercial projects
  • Minimize dependencies: Only add what you need to keep app size small