【问题标题】:Fetch the current user's attributes - Cognito Flutter获取当前用户的属性 - Cognito Flutter
【发布时间】:2023-02-13 02:54:59
【问题描述】:

我正在使用以下函数来获取用户属性。在 iOS 应用程序中,当我运行此功能时,所有数据每次都以不同的顺序出现,例如。 [sub, synchronized, id, email] 下次 [id, sub, email, synchronized]。在 android 应用程序中,所有数据每次都以相同的顺序排列。

Future<void> fetchCurrentUserAttributes() async {
  try {
    final result = await Amplify.Auth.fetchUserAttributes();
      print('sub: ${result[0]}');
      print('synchronized: ${result[1]}');
      print('id: ${result[2]}');
      print('email: ${result[3]}');
  } on AuthException catch (e) {
    print(e.message);
  }
}

有谁知道为什么?我想将这些属性存储在数组中以便在应用程序的其他函数中使用它,因此每次调用此函数时我都需要以相同的顺序排列所有数据。

【问题讨论】:

  • 你为什么不使用一个模型类来存储你完整的未来价值。稍后,您可以调用模型类来提取值(如 sub、id 等)。这样一来,数据的顺序就无关紧要了。
  • 您是否获得异步函数中未等待语句的执行顺序保证?在任何情况下,如果您需要一个已排序的数组,请对其进行排序。不要指望认知中的未记录行为。
  • 更重要的部分是如何将值存储在结果中。就好像它们没有正确存储一样,您永远无法将它们整理好。所以发布将值保存在“结果”数组中的代码。

标签: android ios flutter amazon-cognito aws-amplify


【解决方案1】:

await Amplify.Auth.fetchUserAttributes() 返回 List&lt;AuthUserAttribute&gt;。 您可以将 List 转换为 Map 并通过其键使用它。

  Future<Map<String, String>> getUserAttributes() async {
    final attributes = await Amplify.Auth.fetchUserAttributes();
    final data = {for (var e in attributes) e.userAttributeKey.key: e.value};
    return data;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2022-12-12
    • 2021-05-06
    • 1970-01-01
    • 2021-03-09
    • 2020-10-25
    • 2020-03-09
    相关资源
    最近更新 更多