in

Announcing Version 2.0 of the Flutter SDK for Appwrite!


Hey FlAppwriters, I am super excited to announce version 2.0 of our Flutter SDK with response model support. It is now available on pub.dev. With this release, each of the methods in our Flutter SDKs now has proper response objects. We hope this will help a lot of developers out there who are using Flutter with Appwrite as a tool base for building their applications. Having response objects means you will know what object each endpoint will return and what properties that object has. It will also help IDE have better auto-suggestions. Also, this prevents 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.

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 SDK dependency in your Flutter project’s pubspec.yaml. Add the following dependency:

dependencies:
  appwrite: 2.0.0
Enter fullscreen mode

Exit fullscreen mode

Once you add this dependency and run flutter pub get to get the updated dependencies, you should be able to use it in your Application. Note that this is a huge breaking change, as every call to Appwrite 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 a proper response objects instead. For example account.create() will return a User object where as account.createSession() will return a Session object. All other services will return similar objects in response. If there is an error during API call that you don’t receive the expected message from server, SDK will throw an AppwriteException as before. You can view the details on how each service/method returns by going to our GitHub repository.

Some example code from the SDK:

// Create Account
Future<User> create({required String email, required String password, String? name}) async {
    final String path = '/account';

    final Map<String, dynamic> params = {
        'email': email,
        'password': password,
        'name': name,
    };

    final Map<String, String> headers = {
        'content-type': 'application/json',
    };

    final res = await client.call(HttpMethod.post, path: path, params: params, headers: headers);
    return User.fromMap(res.data);
}
Enter fullscreen mode

Exit fullscreen mode

//User model
class User {
    late final String $id;
    late final String name;
    late final int registration;
    late final int status;
    late final int passwordUpdate;
    late final String email;
    late final bool emailVerification;
    late final PreferencesModel prefs;

    User({
        required this.$id,
        required this.name,
        required this.registration,
        required this.status,
        required this.passwordUpdate,
        required this.email,
        required this.emailVerification,
        required this.prefs,
    });

    factory User.fromMap(Map<String, dynamic> map) {
        return User(
            $id: map['$id'],
            name: map['name'],
            registration: map['registration'],
            status: map['status'],
            passwordUpdate: map['passwordUpdate'],
            email: map['email'],
            emailVerification: map['emailVerification'],
            prefs: Preferences.fromMap(map['prefs']),
        );
    }

    Map<String, dynamic> toMap() {
        return {
            "$id": $id,
            "name": name,
            "registration": registration,
            "status": status,
            "passwordUpdate": passwordUpdate,
            "email": email,
            "emailVerification": emailVerification,
            "prefs": prefs.toMap(),
        };
    }

}
Enter fullscreen mode

Exit fullscreen mode

Note: The endpoint for database documents database.getDocument, will return a Document object which has a method contertTo that you can use to convert data to your own object. The document.list() 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 Flutter.

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



Learn more

You can use followng resources to learn more and get help



Source: https://dev.to/appwrite/announcing-version-2-0-of-the-flutter-sdk-for-appwrite-22hg

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

Animated Flip Card package lets you add an animated flip card to your Flutter app

WildFly 25, JEP 408, Quarkus 2.3.0, MicroProfile 5.0-RC1, Mockito 4.0, Jelastic