Skip to content

Retrieving User Properties

Description

This operation allows retrieving user properties from the BackendlessUser object stored locally in the client application.

Method

// get property value. Return value must be cast to the expected data type.
dynamic getProperty(String key)

// get all user object properties
Map<String, dynamic> get properties

There is a special consideration for a user property containing a collection of related data. For any one-to-many user property, the related data is returned as an array. Before application casts the array object to an array of a specific type, it must check if the size of the array is greater than zero. The following code demonstrates the recommended approach:

// suppose the user object contains a relation user property called "events":
List<dynamic> eventsObjectArray = user.getProperty("events");
List<Map> eventsArray;

// if array is not empty, it can be cast to an array of specific type
if (eventsObjectArray != null && eventsObjectArray.isNotEmpty)
  eventsArray = eventsObjectArray.cast<Map>();

Return Value

An object containing properties and corresponding values.

{  
  "propName1" : value,  
  "propName2": value,   
}

Example

The example below logs in as "alice@yourmail.com" and retrieves the phoneNumber user property.

void loginUserAndGetProperties() {
  Backendless.userService.login("alice@yourmail.com", "wonderland").then((loggedUser) {
    print("User has been logged in: $loggedUser");
    Backendless.userService.currentUser().then((user) {
      if( user != null ) {
        // get user's phone number (i.e. custom property)
        String phoneNumber = user.getProperty("phoneNumber");
        print("phone number: $phoneNumber");
      } else {
        print("User hasn't been logged");
      }
    });
  });
}