Back

[SOLVED] Difference between main() and start() in Dart Server SDK

  • 0
  • Flutter
  • Functions
conqueror
4 May, 2023, 19:40

that is what i am asking why should we use either one of them , and what if i use main instead of start

TL;DR
The difference between `main()` and `start()` in Dart Server SDK is that `main()` is the entry point of the program and `start()` is a function used specifically for cloud functions in Appwrite. In the given code example, `main()` is used because it was written for cloud functions that were set up in the past. However, it is recommended to use `start()` for cloud functions in recent versions of Appwrite. The recommended package to use depends on the platform you are using. For Flutter applications, use the `appwrite` package, and for Dart-only applications, use the `dart_appwrite` package.
Drake
4 May, 2023, 19:44

You must use start. Your code is injected into our server code which runs using main and you can only have one main function. See https://github.com/open-runtimes/open-runtimes/blob/801c6a833aec4ec72fc8562b5dffba2b3967ed6b/runtimes/dart-2.17/server.dart#L37

conqueror
4 May, 2023, 19:48

visited the link but did not find what is the actual use of start

safwan
4 May, 2023, 19:54
TypeScript
await user_code.start(request, response);

In this code, user_code is the code that you write in your cloud function. So when your code is passed into appwrite's server, it looks for the start function in your code, and not main.

Another reason as Steven stated, is that there cannot be 2 main functions in one program. main is where the execution begins, and there can't be two starting points.

safwan
4 May, 2023, 19:55

If you look through the code in the link he sent, you'll see that line 11 has a void main().

safwan
4 May, 2023, 19:56

Hope that makes sense.

conqueror
4 May, 2023, 20:29

got your point, but there is still one doubt, is start is used for getting response and for making request and accessing environment variable?

conqueror
4 May, 2023, 20:29

right ?

safwan
4 May, 2023, 20:33

yes that's correct!

you don't need to worry about the underlying code that runs your cloud function. If you have dependencies and environment variables, you can use them in your function, in the start().

safwan
4 May, 2023, 20:33

For a detailed explanation on dependencies and more in regards to cloud functions, head to this link, and click on the Dart tab to see it's documentation.

https://appwrite.io/docs/functions#writingYourOwnFunction

conqueror
4 May, 2023, 20:36

Thanks man

safwan
4 May, 2023, 20:38

No problem <:appwritecheers:892495536823861258>

conqueror
4 May, 2023, 20:40

as you suggest i have to use start, but in this link why not used start, any suggestion you would like to give and also which package should i use (dart_appwrite or appwrite)https://github.com/lohanidamodar/30daysofappwrite_email_sendgrid/blob/main/main.dart

safwan
4 May, 2023, 20:44

I think the reason that this particular code uses main(), is because that's how the cloud functions were setup at the time. If you notice, the code you sent was updated 2 years ago, which was way before Appwrite v1.x

conqueror
4 May, 2023, 20:44

yes i am aware of that

safwan
4 May, 2023, 20:45

As a matter of fact, I looked at the code examples from 2 years ago, and cloud functions seem to have used main() instead of start()

safwan
4 May, 2023, 20:46

To answer your question about which package to use:

safwan
4 May, 2023, 20:47

If you're using appwrite in a Flutter application, use appwrite: https://pub.dev/packages/appwrite If you're using appwrite in a Dart-only application, use dart_appwrite: https://pub.dev/packages/dart_appwrite

safwan
4 May, 2023, 20:47

Since you're using the server sdk, which supports only Dart, use dart_appwrite

conqueror
4 May, 2023, 20:47

okay,

conqueror
4 May, 2023, 20:48

what are function variable and how to use them and also trigger

safwan
4 May, 2023, 21:02

please make a new post for a different question. helps keep different questions in their own discussion

safwan
4 May, 2023, 21:03

and let me know if you need any more help with the question for this post. if not, I'll mark it as solved 🙂

conqueror
4 May, 2023, 21:07

ok you can marked as solved

safwan
4 May, 2023, 21:08

[SOLVED] Difference between main() and start() in Dart Server SDK

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more