init method
Bootstraps the widget: registers the session with the backend and
returns its configuration (colors, greeting, feature flags), or null
on failure.
Implementation
Future<WidgetConfig?> init() async {
try {
final res = await http
.post(
Uri.parse('$baseUrl/api/chatbot/init'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'widget_key': widgetKey,
'session_id': _sessionId,
..._identityParams,
}),
)
.timeout(const Duration(seconds: 10));
if (res.statusCode == 200) {
final json = jsonDecode(res.body);
if (json['success'] == true) {
final config = WidgetConfig.fromJson(json['data']);
// Read before the socket opens (callers init() first, then
// connectWebSocket()), so _resolvedWsUrl can prefer it.
_configWsUrl = config.wsUrl;
await _cacheColors(config);
if (config.conversationId != null) {
await _saveConversationId(config.conversationId);
}
return config;
}
}
} catch (e) {
debugPrint('[MySampark] init error: $e');
}
return null;
}