Dart In Appwrite
Tags: dart, appwrite, sdk, flutter
Published: 2021-10-31 17:16:56

In the [previous blog](https://dev.to/oshi36/kotlin-in-appwrite-7n2) 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](https://appwrite.io/docs/sdks) is one of the server SDKs in Appwrite but first let's know what is Dart?

[Dart](https://dart.dev/) 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:
```dart
dependencies:
  dart_appwrite: ^1.0.2
```
- You can install packages from the command line:
```dart
pub get dart_appwrite
```

### 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](https://appwrite.io/docs/installation) according to the Operating System and access the Appwrite Console UI

```dart
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
;
```

### Make your first request
```dart
final users = Users(client);

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

res.then((response) {
    print(response);
}).catchError((error) {
    print(error);
});
```


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

- **List functions** : To get a list of all the project's functions. You can use the query params to filter your results.
```dart
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);
  });
}
```

- **Get function** : It will get a function by its unique ID.
```dart
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);
  });
}
```
- **Update function** : It will update the function by its unique ID.
```dart
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);
  });
}
```

- **Delete function** : It will delete a function by its unique ID.
```dart
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);
  });
}
```

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](https://appwrite.io/docs/getting-started-for-server)

**Thank You**