updateProfile method

Future<UserAccount> updateProfile({
  1. required String email,
  2. String? username,
  3. String? bio,
})

Implementation

Future<UserAccount> updateProfile({
  required String email,
  String? username,
  String? bio,
}) async {
  final users = await _readUsers();
  final index = users.indexWhere((u) => u.email == email);
  if (index < 0) {
    throw StateError('user not found');
  }
  final user = users[index];
  if (username != null &&
      username.isNotEmpty &&
      users.any((u) => u.username == username && u.email != email)) {
    throw StateError('username already exists');
  }
  final updated = user.copyWith(
    username: username ?? user.username,
    bio: bio ?? user.bio,
    updatedAt: DateTime.now(),
  );
  users[index] = updated;
  await _writeUsers(users);
  await addActivity(
    email: email,
    action: 'profile_update',
    message: 'Updated profile',
  );
  return updated;
}