A lightweight Dart SDK for fetching configuration from ToggLit with fallback support.
Add the package to your pubspec.yaml:
dependencies:
togglit_sdk: ^0.0.3Then run:
flutter pub getimport 'package:togglit_sdk/togglit_sdk.dart';
void main() async {
final config = await TogglitConfig.getConfig(
apiKey: "tk_live_xxxxxxx",
projectId: "togglit-demo",
env: "production",
version: 1,
fallback: {
"feature_flag": false,
"api_url": "https://api.fallback.com",
},
);
print(config);
}Fetches configuration from ToggLit with automatic fallback support and comprehensive error handling.
| Parameter | Type | Required | Description |
|---|---|---|---|
| apiKey | String | ✅ | Your Togglit API key |
| projectId | String | ✅ | Your project identifier |
| env | String | ✅ | Environment name (e.g., 'production', 'dev') |
| version | int? | ❌ | Specific configuration version to fetch |
| fallback | Map<String, dynamic>? | ❌ | Fallback configuration object (default: ) |
Future<Map<String, dynamic>> - The configuration object or fallback if request fails.
final config = await TogglitConfig.getConfig(
apiKey: "tk_live_xxxxxxx",
projectId: "my-app",
env: "production",
);final config = await TogglitConfig.getConfig(
apiKey: "tk_live_xxxxxxx",
projectId: "my-app",
env: "production",
fallback: {
"enableBetaFeatures": false,
"maintenanceMode": false,
"apiUrl": "https://fallback.example.com",
"timeout": 5000,
},
);final config = await TogglitConfig.getConfig(
apiKey: "tk_live_xxxxxxx",
projectId: "my-app",
env: "production",
fallback: {
"enableBetaFeatures": false,
"enableDarkMode": true,
"showOnboarding": true,
},
);
// Use feature flags in your app
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: config["enableDarkMode"] ? ThemeData.dark() : ThemeData.light(),
home: config["showOnboarding"] ? OnboardingScreen() : HomeScreen(),
);
}
}class ConfiguredApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder<Map<String, dynamic>>(
future: TogglitConfig.getConfig(
apiKey: "tk_live_xxxxxxx",
projectId: "my-app",
env: "production",
fallback: {"loading": false},
),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
final config = snapshot.data ?? {};
return MyApp(config: config);
},
);
}
}The SDK includes comprehensive error handling to ensure your app continues to function even when configuration fetching fails:
final config = await TogglitConfig.getConfig(
apiKey: "invalid-key",
projectId: "my-app",
env: "production",
fallback: {
"feature_flag": false,
"retry_limit": 3,
"maintenance_mode": false,
},
);
// Even with invalid API key, config will contain the fallback values
// Your app continues to work with sensible defaults