Dart In Appwrite

oshi36

Oshi Gupta

Posted on October 31, 2021

Dart In Appwrite

In the previous blog we have seen about how to use Kotlin in Appwrite. Now we will be seeing about how to use Dart in Appwrite.

As we Dart SDK is one of the server SDKs in Appwrite but first let's know what is Dart?

Dart is an open-source, general-purpose, object-oriented programming language with C-style syntax developed by Google in 2011. The purpose of Dart programming is to create a frontend user interfaces for the web and mobile apps.

So now how to write Dart in Appwrite function and how to install Dart SDK for it.

Dart SDK Installation

  • Add this to your package's pubspec.yaml file:
dependencies:
  dart_appwrite: ^1.0.2
Enter fullscreen mode Exit fullscreen mode
  • You can install packages from the command line:
pub get dart_appwrite
Enter fullscreen mode Exit fullscreen mode

Add the function

You can add a new function from your Appwrite project's dashboard. Access your Functions settings from your project's left navigation panel. Click the Add Function button and choose your function name and code runtime.

Init your SDK

Initialize your SDK with your Appwrite server API endpoint and project ID which can be found in your project settings page and your new API secret Key project API keys section.
This can be found out when you install Appwrite according to the Operating System and access the Appwrite Console UI

import 'package:dart_appwrite/dart_appwrite.dart';

Client client = Client();
client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
;
Enter fullscreen mode Exit fullscreen mode

Make your first request

final users = Users(client);

final res = users.create('email@example.com', 'password');

res.then((response) {
    print(response);
}).catchError((error) {
    print(error);
});
Enter fullscreen mode Exit fullscreen mode

Now, we will be seeing different operations which can be performed on these functions.

  • Create functions : To create a new function by passing various parameters like name , runtime etc.
import 'package:dart_appwrite/dart_appwrite.dart';

void main() { // Init SDK
  Client client = Client();
  Functions functions = Functions(client);

  client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
  ;

  Future result = functions.create(
    name: '[NAME]',
    execute: [],
    runtime: 'dotnet-5.0',
  );

  result
    .then((response) {
      print(response);
    }).catchError((error) {
      print(error.response);
  });
}
Enter fullscreen mode Exit fullscreen mode
  • List functions : To get a list of all the project's functions. You can use the query params to filter your results.
import 'package:dart_appwrite/dart_appwrite.dart';

void main() { // Init SDK
  Client client = Client();
  Functions functions = Functions(client);

  client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
  ;

  Future result = functions.list(
  );

  result
    .then((response) {
      print(response);
    }).catchError((error) {
      print(error.response);
  });
}
Enter fullscreen mode Exit fullscreen mode
  • Get function : It will get a function by its unique ID.
import 'package:dart_appwrite/dart_appwrite.dart';

void main() { // Init SDK
  Client client = Client();
  Functions functions = Functions(client);

  client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
  ;

  Future result = functions.get(
    functionId: '[FUNCTION_ID]',
  );

  result
    .then((response) {
      print(response);
    }).catchError((error) {
      print(error.response);
  });
}
Enter fullscreen mode Exit fullscreen mode
  • Update function : It will update the function by its unique ID.
import 'package:dart_appwrite/dart_appwrite.dart';

void main() { // Init SDK
  Client client = Client();
  Functions functions = Functions(client);

  client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
  ;

  Future result = functions.update(
    functionId: '[FUNCTION_ID]',
    name: '[NAME]',
    execute: [],
  );

  result
    .then((response) {
      print(response);
    }).catchError((error) {
      print(error.response);
  });
}
Enter fullscreen mode Exit fullscreen mode
  • Delete function : It will delete a function by its unique ID.
import 'package:dart_appwrite/dart_appwrite.dart';

void main() { // Init SDK
  Client client = Client();
  Functions functions = Functions(client);

  client
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
  ;

  Future result = functions.delete(
    functionId: '[FUNCTION_ID]',
  );

  result
    .then((response) {
      print(response);
    }).catchError((error) {
      print(error.response);
  });
}
Enter fullscreen mode Exit fullscreen mode

So this is the basic idea of how to create functions in the Appwrite using the Dart SDK.

For more you can always refer to Appwrite docs

Thank You

💖 💪 🙅 🚩
oshi36
Oshi Gupta

Posted on October 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Dart In Appwrite
dart Dart In Appwrite

October 31, 2021