class User { const User({ required this.id, this.email, this.phone, required this.role, this.routeId, }); final String id; final String? email; final String? phone; final String role; final String? routeId; factory User.fromJson(Map json) { return User( id: _pickString([json['id'], json['user_id'], json['userId']]) ?? '', email: _pickString([json['email'], json['mail']]), phone: _pickString([json['phone'], json['telefono']]), role: _pickString([json['role'], json['rol']]) ?? 'citizen', routeId: _pickString([json['routeId'], json['route_id']]), ); } Map toJson() { return { 'id': id, 'email': email, 'phone': phone, 'role': role, 'routeId': routeId, }; } static String? _pickString(Iterable candidates) { for (final candidate in candidates) { if (candidate is String && candidate.isNotEmpty) { return candidate; } if (candidate != null && candidate.toString().isNotEmpty) { return candidate.toString(); } } return null; } }