1

I am trying to use appwrite server sdk list users to get userid from an email.

The documentation says there is a search: option that can be used but no where does it say what the format of that String? is.

What is the format of the search: String? to only get a list of users whose email matches?

void main() { // Init SDK
  Client client = Client();
  Users users = Users(client);

  client
    .setEndpoint(endPoint) // Your API Endpoint
    .setProject(projectID) // Your project ID
    .setKey(apiKey) // Your secret API key
  ;

  Future result = users.list(search: '<<<WHAT GOES HERE>>>');
}

2 Answers2

3

:wave: Hello!

Thanks for bringing this question up, this is definitely not well documented, I'll note this down and try to make it clearer in the docs, but here's how you'd approach this in Dart:

  final res = users.list(search: Query.equal('email', 
    'email@example.com'));

  res.then((response) {
    print(response.users[0].toMap());
  }).catchError((error) {
    print(error);
  });

The Query object generates a query string, and works similar to how listDocument would work. The difference here is that it only takes a single query string instead of a list.

Vincent Ge
  • 111
  • 3
0

Appwrite doesn't allow to get users record. Rather you have to add a separate package for it, which is dar_appwrite or you can get users by API call.

Here is the example:

Future<dynamic?> fetchUsers() {

  String baseURL = "http://[HOST NAME]/v1/users/";

  Map<String, String> headers = {
      "X-Appwrite-Project": "[PROJECT_ID]",
      "X-Appwrite-Key": "[SECRET_KEY",
    };
    
  final response = http.get(Uri.parse(baseURL), headers:headers);
    
   if (response.statusCode == 200) {
       return jsonDecode(response.body);
     } else {
       throw Exception('Failed to get Users');
     }
   }
}

If you want to filter the record then this is how you can pass parameters in it:

Future<dynamic?> fetchUsers() {

  String baseURL = "http://[HOST NAME]/v1/users/";

  Map<String, String> headers = {
     "X-Appwrite-Project": "[PROJECT_ID]",
     "X-Appwrite-Key": "[SECRET_KEY",
   };

  Map<String, String> queryParams = {
       'queries[]': 'limit(10)',
   };
    
  final response = http.get(Uri.parse(baseURL).replace(queryParameters:queryParams), headers:headers);
    
   if (response.statusCode == 200) {
       return jsonDecode(response.body);
     } else {
       throw Exception('Failed to get Users');
     }
   }
}