You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
127
-
.setProject("5df5acd0d48c2") // Your project ID
126
+
.setEndpoint("https://<HOSTNAME_OR_IP>/v1") // Your API Endpoint
127
+
.setProject("<PROJECT_ID>") // Your project ID
128
128
.setSelfSigned(true) // Remove in production
129
129
130
130
val account =Account(client)
@@ -136,7 +136,134 @@ val user = account.create(
136
136
)
137
137
```
138
138
139
+
### Type Safety with Models
140
+
141
+
The Appwrite Android SDK provides type safety when working with database documents through generic methods. Methods like `listDocuments`, `getDocument`, and others accept a `nestedType` parameter that allows you to specify your custom model type for full type safety.
142
+
143
+
**Kotlin:**
144
+
```kotlin
145
+
data classBook(
146
+
valname:String,
147
+
valauthor:String,
148
+
valreleaseYear:String? = null,
149
+
valcategory:String? = null,
150
+
valgenre:List<String>? = null,
151
+
valisCheckedOut:Boolean
152
+
)
153
+
154
+
val databases =Databases(client)
155
+
156
+
try {
157
+
val documents = databases.listDocuments(
158
+
databaseId ="your-database-id",
159
+
collectionId ="your-collection-id",
160
+
nestedType =Book::class.java // Pass in your custom model type
161
+
)
162
+
163
+
for (book in documents.documents) {
164
+
Log.d("Appwrite", "Book: ${book.name} by ${book.author}") // Now you have full type safety
**Tip**: You can use the `appwrite types` command to automatically generate model definitions based on your Appwrite database schema. Learn more about [type generation](https://appwrite.io/docs/products/databases/type-generation).
226
+
227
+
### Working with Model Methods
228
+
229
+
All Appwrite models come with built-in methods for data conversion and manipulation:
230
+
231
+
**`toMap()`** - Converts a model instance to a Map format, useful for debugging or manual data manipulation:
232
+
```kotlin
233
+
val account =Account(client)
234
+
val user = account.get()
235
+
val userMap = user.toMap()
236
+
Log.d("Appwrite", userMap.toString()) // Prints all user properties as a Map
237
+
```
238
+
239
+
**`from(map:, nestedType:)`** - Creates a model instance from a Map, useful when working with raw data:
**JSON Serialization** - Models can be easily converted to/from JSON using Gson (which the SDK uses internally):
250
+
```kotlin
251
+
importcom.google.gson.Gson
252
+
253
+
val account =Account(client)
254
+
val user = account.get()
255
+
256
+
// Convert to JSON
257
+
val gson =Gson()
258
+
val jsonString = gson.toJson(user)
259
+
Log.d("Appwrite", "User JSON: $jsonString")
260
+
261
+
// Convert from JSON
262
+
val userFromJson = gson.fromJson(jsonString, User::class.java)
263
+
```
264
+
139
265
### Error Handling
266
+
140
267
The Appwrite Android SDK raises an `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.
0 commit comments