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 h…


This content originally appeared on DEV Community and was authored by Damodar Lohani

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

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);
}
//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(),
        };
    }

}

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

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


This content originally appeared on DEV Community and was authored by Damodar Lohani


Print Share Comment Cite Upload Translate Updates
APA

Damodar Lohani | Sciencx (2021-10-11T05:34:50+00:00) Announcing Version 2.0 of the Flutter SDK for Appwrite!. Retrieved from https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/

MLA
" » Announcing Version 2.0 of the Flutter SDK for Appwrite!." Damodar Lohani | Sciencx - Monday October 11, 2021, https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/
HARVARD
Damodar Lohani | Sciencx Monday October 11, 2021 » Announcing Version 2.0 of the Flutter SDK for Appwrite!., viewed ,<https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/>
VANCOUVER
Damodar Lohani | Sciencx - » Announcing Version 2.0 of the Flutter SDK for Appwrite!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/
CHICAGO
" » Announcing Version 2.0 of the Flutter SDK for Appwrite!." Damodar Lohani | Sciencx - Accessed . https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/
IEEE
" » Announcing Version 2.0 of the Flutter SDK for Appwrite!." Damodar Lohani | Sciencx [Online]. Available: https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/. [Accessed: ]
rf:citation
» Announcing Version 2.0 of the Flutter SDK for Appwrite! | Damodar Lohani | Sciencx | https://www.scien.cx/2021/10/11/announcing-version-2-0-of-the-flutter-sdk-for-appwrite/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.