Flutter: Navigation, Networking & Platform
Navigation (GoRouter — recommended)
// pubspec.yaml: go_router: ^13.0.0
final router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(path: '/', builder: (context, state) => const HomeScreen()),
GoRoute(
path: '/users/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return UserDetailScreen(userId: id);
},
),
ShellRoute(
builder: (context, state, child) => AppShell(child: child),
routes: [
GoRoute(path: '/feed', builder: (_, __) => const FeedScreen()),
GoRoute(path: '/profile', builder: (_, __) => const ProfileScreen()),
],
),
],
redirect: (context, state) {
final isLoggedIn = ref.read(authProvider).isAuthenticated;
if (!isLoggedIn && state.matchedLocation != '/login') return '/login';
return null;
},
);
// Navigate
context.go('/users/123') // replace current
context.push('/users/123') // push onto stack
context.pop() // go back
context.goNamed('userDetail', pathParameters: {'id': '123'})HTTP & Networking
// pubspec.yaml: http: ^1.2.0 (or dio: ^5.4.0 for interceptors)
import 'package:http/http.dart' as http;
import 'dart:convert';
class ApiService {
static const _baseUrl = 'https://api.example.com';
final String _token;
ApiService(this._token);
Future<User> getUser(String id) async {
final response = await http.get(
Uri.parse('$_baseUrl/users/$id'),
headers: {'Authorization': 'Bearer $_token', 'Content-Type': 'application/json'},
);
if (response.statusCode == 200) {
return User.fromJson(jsonDecode(response.body));
} else if (response.statusCode == 404) {
throw UserNotFoundException(id);
} else {
throw ApiException(response.statusCode, response.body);
}
}
Future<User> createUser(CreateUserDto dto) async {
final response = await http.post(
Uri.parse('$_baseUrl/users'),
headers: {'Authorization': 'Bearer $_token', 'Content-Type': 'application/json'},
body: jsonEncode(dto.toJson()),
);
return User.fromJson(jsonDecode(response.body));
}
}
// JSON serialization with freezed/json_serializable
// pubspec.yaml: freezed_annotation, json_annotation, build_runner, freezed, json_serializable
@freezed
class User with _$User {
const factory User({
required String id,
required String name,
required String email,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}Platform Channels & Plugins
Platform channels: bidirectional communication between Dart and native (iOS/Android) code
pub.dev: official package repository — search before writing native code
Popular plugins: camera, geolocator, local_notifications, shared_preferences, sqflite (SQLite), firebase_core, in_app_purchase
shared_preferences: key-value storage (UserDefaults on iOS, SharedPreferences on Android)
sqflite: SQLite for Flutter — local structured data storage
firebase_core + firebase_auth + cloud_firestore: Firebase integration
// shared_preferences — persistent key-value storage
import 'package:shared_preferences/shared_preferences.dart';
final prefs = await SharedPreferences.getInstance();
await prefs.setString('token', token);
final token = prefs.getString('token');
await prefs.remove('token');
// Platform channel (calling native code)
const _channel = MethodChannel('com.example.myapp/biometric');
Future<bool> authenticateWithBiometric() async {
try {
return await _channel.invokeMethod<bool>('authenticate') ?? false;
} on PlatformException catch (e) {
print('Biometric failed: ${e.message}');
return false;
}
}Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free