updateProfile method

Future<User> updateProfile(
  1. String? nickname,
  2. String? avatar,
  3. String? description
)

Implementation

Future<User> updateProfile(
    String? nickname, String? avatar, String? description) async {
  //
  var body = json.encode({
    "nickname": nickname,
    "avatar": avatar,
    "description": description,
    "client": client
  });
  //
  final initUrl =
      BytedeskUtils.getHostUri('/api/user/update/visitor/profile');
  final initResponse =
      await httpClient.post(initUrl, headers: getHeaders(), body: body);
  //解决json解析中的乱码问题
  Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
  //将string类型数据 转换为json类型的数据
  final responseJson =
      json.decode(utf8decoder.convert(initResponse.bodyBytes));
  debugPrint("updateProfile:$responseJson");
  // 判断token是否过期
  if (responseJson.toString().contains('invalid_token')) {
    bytedeskEventBus.fire(InvalidTokenEventBus());
  }
  // 更新本地数据
  SpUtil.putString(BytedeskConstants.nickname, nickname!);
  SpUtil.putString(BytedeskConstants.avatar, avatar!);
  SpUtil.putString(BytedeskConstants.description, description!);

  return User.fromJson(responseJson['data']);
}