DEV Community

Cover image for Announcing Appwrite Dart SDK 2.0
Damodar Lohani for Appwrite

Posted on

Announcing Appwrite Dart SDK 2.0

Hey Appwriters πŸ‘‹ we are super excited to announce version 2.0 of our Dart SDK with support for response models! πŸ₯³ Back in October, we announced response models support in our Flutter SDK and we've now extended the same to our Dart SDK available on pub.dev.

With this release, each service and method in the Dart SDK will return a structured response object rather than a simple JSON enabling you to know exactly what each method returns and what properties the response contains. It will also support better auto-suggestions in IDEs. This further helps with preventing bugs that might occur while trying to access non-existent properties on JSON objects. Finally, this means you will no longer have to create your own response objects!

🏁 Get Started!

If you're new to the Appwrite Dart SDK, unlike the Appwrite Flutter SDK, this SDK was designed for integrating with Appwrite directly from your Dart server-side code or with Appwrite's Cloud Functions using the dedicated Dart runtime environment. It uses an API key with elevated permissions or a JWT for authentication and allows you to act as an admin and manage your Appwrite project.

Now, let's dive in on how you can use this SDK in your project and how it looks.

First, you need to add Appwrite's Dart SDK dependency in your Dart project's pubspec.yaml. Add the following dependency:

dependencies:
  dart_appwrite: 2.0.0
Enter fullscreen mode Exit fullscreen mode

Once you add this dependency and run dart pub get to get the updated dependencies, you should be able to use it in your application.

Note that this will result in a breaking change, as every call from Appwrite's Dart SDK to any service previously returned a Response object with data as JSON object received from server. However with this release, every endpoint will return structured response objects instead.

For example database.createCollection() will return a Collection object where as database.listCollections() will return a CollectionList object. All other services will return similar objects in their response. If there is any error during the API call, the SDK will throw an AppwriteException object as before. You can view the details on what each service/method returns by visiting our GitHub repository.

Previously any call from the SDK was returning Response object with json data. However with this update you will get a related response object. So you will no longer have to create your own data classes and parse the json. You will know exactly what to expect from each SDK call. For example if you previously had a code to get a collection from server using its ID and print the name of the collection, it would look something like this.

Future<void> getCollection() async {
    try {
        final Map<String, dynamic> res = await database.getCollection(collectionId: '063dasee342ase');
        print(res.data['name']);
    } on AppwriteException(catch e) {
        print(e.message);
    }
}
Enter fullscreen mode Exit fullscreen mode

The problem with this is, we don't exactly know what keys are available in the JSON response and what their types are. There is no auto completion or suggestions from IDE either. However with this update, the same code can be modified to the following.

import 'package:appwrite/models.dart';

Future<void> getCollection() async {
  try {
        final Collection collection = await database.getCollection(collectionId: '063dasee342ase');
        print(collection.name);
    } on AppwriteException(catch e) {
        print(e.message);
    }  
}
Enter fullscreen mode Exit fullscreen mode

Now you know exactly what to expect by looking at the definition of the Collection class. The also enables the IDE to suggest auto completions while trying to access properties of collection and you no longer have to create your own data classes. Isn't that a time saver?

Note: The endpoint for database documents database.getDocument, will return a Document object which has a method convertTo that you can use to convert data to your own object. The document.list() method works in the similar way.

For example:

database.getDocument(collectionId: "collectionId", documentId: "documentId").convertTo<Movie>(Movie.fromJson) //will return a Movie object

database.listDocuments(collectionId: "collectionId").convertTo<Movie>(Movie.fromJson) // will return a List<Movie> object
Enter fullscreen mode Exit fullscreen mode

For a complete example, please visit our Playground For Dart.

If you have any issues or questions feel free to reach out to us on our discord

πŸ“š Learn more

You can use following resources to learn more and get help

Top comments (0)