Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

カスタムユーザーデータ - Flutter SDK

項目一覧

  • 始める前に
  • ユーザーのカスタム ユーザー データの読み取り
  • Atlas Function を使用したカスタムユーザーデータの書込み

Atlas App Services を使用すると、ユーザーに関する任意のカスタム データを保存できます。 たとえば、ユーザーの希望言語、誕生日、ローカル タイムゾーン を保存できます。 このデータを読み書きする前に、バックエンドでカスタム ユーザー データを有効にする必要があります。 詳しくは、「 カスタム ユーザー データの有効化 」を参照してください。

重要

現在、Flutter SDK でカスタム ユーザー データのみを読み取ることができます。 SDK への将来のアップデートでは、SDK からカスタム ユーザー データも書き込むことができるようになります。

他の Realm SDK、Atlas Functions、または Atlas を直接クエリすることで、カスタム ユーザー データを作成、アップデート、削除できます。

カスタム ユーザー データを使用するには、まず Atlas App Services でカスタム ユーザー データを有効にする必要があります。

  1. アプリを作成します。

  2. カスタム ユーザー データを有効にします。

ユーザー .customData でカスタム ユーザー データを取得します。 ログインしたユーザーの プロパティ。

final customUserData = user.customData;

App Services は、基礎となるデータが変更されたときにすぐに User.customDataの値を動的に更新しません代わりに、ユーザーが アクセス トークン を更新するたび、または ユーザー .refreshCustomData() を明示的に呼び出すと、App Services は最新バージョンのカスタム ユーザー データを取得します。 は、アプリが最新のカスタム ユーザー データを保持するようにします。

// refreshCustomData() returns the updated custom data object
final updatedCustomData = await user.refreshCustomData();
// Now when you access User.customData it's the value
// returned from User.refreshCustomData()

Atlas Function を使用してカスタムユーザーデータに書込むことができます。 Atlas Function は、バックエンド アプリに構築されるサーバーサイドの JavaScript 関数です。 Atlas Function は Realm Flutter SDK から直接呼び出せます。

Realm Flutter SDK からカスタム ユーザー データに直接書き込むことはできません。

Atlas Function の詳細については、次のドキュメントを参照してください。

Atlas Function からカスタム ユーザー データを追加するための単一パターンはありません。 アプリケーションのユースケースに合わせて関数を記述する必要があります。

この例では、Atlas Function はクライアントによって渡されたオブジェクトを受け取り、Atlas のカスタムユーザーデータコレクションに追加されます。 関数は、カスタム ユーザー データがまだ存在しない場合はそれを作成し、存在する場合はその中のすべてのデータを置き換えます。

writeCustomUserData.js - サーバー上で実行中の Atlas Function(JavaScript)
exports = async function writeCustomUserData(newCustomUserData) {
const userId = context.user.id;
const customUserDataCollection = context.services
.get("mongodb-atlas")
.db("custom-user-data-database")
.collection("custom-user-data");
const filter = { userId };
// Replace the existing custom user data document with the new one.
const update = { $set: newCustomUserData };
// Insert document if it doesn't already exist
const options = { upsert: true };
const res = await customUserDataCollection.updateOne(filter, update, options);
return res;
};

この関数を呼び出す Realm SDK コードは次のとおりです。

Realm Flutter SDK クライアント コード(Dart)
final user = app.currentUser!;
final updatedTimestamp = DateTime.now().millisecondsSinceEpoch;
final updatedCustomUserData = {
"userId": user.id,
"favoriteFood": "pizza",
"lastUpdated": updatedTimestamp
};
final functionResponse = await user.functions
.call("writeCustomUserData", [updatedCustomUserData]);
// Contains the `updatedCustomUserData` object just added
// in the above Atlas Function call
final customUserData = await user.refreshCustomData();

戻る

ユーザー ID のリンク