Salesforce Integration with Flutter: A Complete Guide
Introduction
In today’s mobile-first world, integrating powerful backend systems like Salesforce CRM with a sleek frontend built in Flutter opens doors to scalable and efficient business apps. Whether it’s syncing leads, managing contacts, or scheduling test drives, Salesforce integration brings real-time enterprise data into your hands via mobile.
In this blog, we’ll walk through how to connect a Flutter mobile app with Salesforce using its REST APIs.
Prerequisites
– Salesforce Developer Account: https://developer.salesforce.com/signup
– Basic knowledge of Flutter & Dart
– Flutter SDK and IDE (e.g., VS Code or Android Studio)
– Salesforce Connected App with OAuth credentials
Step 1: Create a Connected App in Salesforce
- Go to Setup in Salesforce.
- Search for App Manager → Click New Connected App.
- Fill in:
– App Name: Flutter Integration
– Email: your-email@example.com - Under API (Enable OAuth Settings):
– Check Enable OAuth Settings
– Set Callback URL: https://login.salesforce.com/services/oauth2/callback
– Select Scopes: Full access (full), Perform requests on your behalf (refresh_token, offline_access) - Click Save → Note the Consumer Key and Consumer Secret.
Step 2: Authenticate from Flutter (Password OAuth Flow)
⚠️ Use this for testing only. For production, use Web or Device OAuth flow.
Future<void> authenticateWithSalesforce() async {
const String loginUrl = ‘https://login.salesforce.com/services/oauth2/token’;
final response = await http.post(Uri.parse(loginUrl), body: {
‘grant_type’: ‘password’,
‘client_id’: ‘<YOUR_CLIENT_ID>’,
‘client_secret’: ‘<YOUR_CLIENT_SECRET>’,
‘username’: ‘<YOUR_USERNAME>’,
‘password’: ‘<YOUR_PASSWORD><SECURITY_TOKEN>’,
});
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
final accessToken = data[‘access_token’];
final instanceUrl = data[‘instance_url’];
// Store and use these for API calls
} else {
print(‘Authentication failed: ${response.body}’);
}
}
Step 3: Make API Requests to Salesforce
Example: Fetch Leads
Future<void> fetchLeads(String accessToken, String instanceUrl) async {
final url = ‘$instanceUrl/services/data/v59.0/query/?q=SELECT+Name,Company+FROM+Lead’;
final response = await http.get(
Uri.parse(url),
headers: {‘Authorization’: ‘Bearer $accessToken’},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
print(data[‘records’]);
} else {
print(‘Error fetching leads: ${response.body}’);
}
}
Example: Create a New Lead
Future<void> createLead(String accessToken, String instanceUrl) async {
final url = ‘$instanceUrl/services/data/v59.0/sobjects/Lead/’;
final response = await http.post(Uri.parse(url),
headers: {
‘Authorization’: ‘Bearer $accessToken’,
‘Content-Type’: ‘application/json’,
},
body: jsonEncode({
‘LastName’: ‘Doe’,
‘Company’: ‘Example Inc’,
‘Status’: ‘Open – Not Contacted’
}),
);
if (response.statusCode == 201) {
print(‘Lead created successfully.’);
} else {
print(‘Failed to create lead: ${response.body}’);
}
}
Test the Integration
You can now build UIs in Flutter to:
– Login using Salesforce credentials
– Display Leads or Contacts in a ListView
– Add new records via forms
Pro Tip: Build Beautiful UIs
Use modern Flutter widgets like ListView.builder, Card, and TextFormField with validation to make your CRM app look professional and user-friendly.
Security Considerations
– Never hardcode credentials in production.
– Use secure storage (like Flutter Secure Storage) for tokens.
– Prefer OAuth with PKCE or Web View for production.
Conclusion
Salesforce + Flutter is a powerful combo for enterprise mobility. By integrating with the Salesforce REST API, you can build robust mobile solutions that extend CRM capabilities to your users’ pockets.
Whether you’re building for dealers, sales reps, or service agents, this integration makes your app truly enterprise-grade.
Resources
– Salesforce REST API Docs: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/
– Flutter Documentation: https://flutter.dev/docs
– Secure Storage in Flutter: https://pub.dev/packages/flutter_secure_storage