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 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:
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 convertTo
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
- ๐ Getting Started Tutorial
- ๐ Appwrite Docs
- ๐ฌ Discord Community
- ๐ Appwrite Flutter Playground
Top comments (4)
Beautiful. I wish we could have some complete FlAppWrite tutorials on clones like social networking, messaging and e-commerce sites; using state management tools like Bloc, GetX, Riverpod and implemented with Architectures like Clean, MVVM, MVCโฆ
10 good courses of like 3hrs per course could really help adoption of FlAppWrite, and reduce the stress of people moving from Beginner to Intermediate (without having to pass through Firebase because of their abundance of learning materials).
Super useful FlAppWrite muscle building ๐
Awesome! Great job!
contertTo should be convertTo I assume.
Thanks. Corrected!