Prompt
How do I integrate a mobile backend with Flutter?
Latest observation
Integrating a mobile backend with Flutter usually means connecting your app to an API or backend service for authentication, data storage, push notifications, file uploads, etc. The exact setup depends on whether you’re using a custom backend or a Backend-as-a-Service like Firebase/Supabase.
Common ways to integrate
1. REST API backend
If your backend exposes HTTP endpoints:
- Use Flutter’s
httppackage ordio - Send requests to your backend
- Parse JSON responses into Dart models
- Handle auth with tokens
Example with http:
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<List<dynamic>> fetchItems() async {
final response = await http.get(
Uri.parse('https://api.example.com/items'),
headers: {'Authorization': 'Bearer your_token'},
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception('Failed to load items');
}
}
2. GraphQL backend
If your backend uses GraphQL:
- Use
graphql_flutter - Define queries and mutations
- Fetch only the data you need
3. Firebase backend
If you want faster setup:
- Use
firebase_core - Add packages like:
firebase_authcloud_firestorefirebase_storagefirebase_messaging
This is useful for auth, realtime database, file storage, and push notifications.
4. Supabase backend
If you want an open-source alternative:
- Use the
supabase_flutterpackage - Get auth, database, storage, and realtime features with less custom backend work
Typical integration steps
Step 1: Add dependencies
In pubspec.yaml:
dependencies:
flutter:
sdk: flutter
http: ^1.2.0
Run:
flutter pub get
Step 2: Create API service
Keep backend calls in a separate file, e.g. api_service.dart:
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiService {
final String baseUrl = 'https://api.example.com';
Future<List<dynamic>> getUsers() async {
final response = await http.get(Uri.parse('$baseUrl/users'));
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception('Failed to fetch users');
}
}
}
Step 3: Call backend from UI
Use FutureBuilder, Provider, Riverpod, Bloc, or setState depending on your app architecture.
FutureBuilder(
future: ApiService().getUsers(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
final users = snapshot.data as List<dynamic>;
return ListView.builder(
itemCount: users.length,
itemBuilder: (_, index) {
return ListTile(title: Text(users[index]['name']));
},
);
}
},
)
Authentication
Usually your backend will issue a token after login.
- Store tokens securely using
flutter_secure_storage - Send token in request headers
headers: {
'Authorization': 'Bearer $token',
}
Best practices
- Keep API logic out of widgets
- Use model classes for JSON parsing
- Handle errors and loading states
- Use HTTPS
- Store sensitive values securely
- Consider interceptors for auth refresh if using
dio
Recommended architecture
A clean setup often looks like this:
- UI layer: widgets
- State management: Provider/Riverpod/Bloc
- Repository layer: communicates with backend
- Service layer: performs HTTP/GraphQL/Firebase calls
- Models: represent API data
If you want, I can show you:
- a full Flutter + REST API example,
- Flutter + Firebase setup, or
- Flutter + Supabase setup.