Announcing Version 2.0 of the Flutter SDK for Appwrite!
Tags: flutter, appwrite, announcement, news
Published: 2021-10-11 05:34:50

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](https://pub.dev/packages/appwrite). With this release, each of the methods in our Flutter SDKs now has structured 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:

```yaml
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](https://github.com/appwrite/sdk-for-flutter).

Some example code from the SDK:

```dart
// 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);
}
```

```dart
//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 `convertTo` that you can use to convert data to your own object. The `document.list()` works in the similar way.

For example:

```dart
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](https://github.com/appwrite/playground-for-flutter).

If you have any issues or questions feel free to reach us on [our discord](https://appwrite.io/discord)

## Learn more

You can use followng resources to learn more and get help
- 🚀 [Getting Started Tutorial](https://appwrite.io/docs/getting-started-for-flutter)
- 📜 [Appwrite Docs](https://appwrite.io/docs)
- 💬 [Discord Community](https://appwrite.io/discord)
- 🚂 [Appwrite Flutter Playground](https://github.com/appwrite/playground-for-flutter)