Back

Type support for dart cloud functions

  • 0
  • Flutter
  • Functions
Ipsoka
28 Mar, 2023, 18:58

Hey, currently I dont seem to be able to find a way to give req and res in Future<void> start(final req, final res) a type which is pretty annoying. Are there any types for req and res I can use?`Something like AppwriteRequest or AppwriteResponse

TL;DR
There is currently no built-in type support for req and res in Dart cloud functions. However, you can use a workaround by defining your own AppwriteRequest and AppwriteResponse classes. Here is an example implementation: ```dart class AppwriteRequest { final Map<String, dynamic> variables; final Map<String, dynamic> headers; final String payload; AppwriteRequest({ this.variables = const {}, this.headers = const {}, this.payload = '', }); } class AppwriteResponse { int _status = 200; dynamic _text; int get status => _status; dynamic get
Drake
28 Mar, 2023, 19:34

We've had discussions about exposing the types. For now, maybe you can copy and paste this and cast the variables in your function body.

https://github.com/open-runtimes/open-runtimes/blob/main/runtimes/dart-2.17/function_types.dart

Ipsoka
28 Mar, 2023, 20:26

Casting doesnt work, 'im getting type 'Response' is not a subtype of type 'AppwriteResponse' in type cast'

Ipsoka
28 Mar, 2023, 20:27
TypeScript
class AppwriteRequest {
  final Map<String, dynamic> variables;
  final Map<String, dynamic> headers;
  final String payload;

  AppwriteRequest({
    this.variables = const {},
    this.headers = const {},
    this.payload = '',
  });
}

class AppwriteResponse {
  int _status = 200;
  dynamic _text;

  int get status => _status;
  dynamic get body => _text;

  AppwriteResponse send(String? text, {int status = 200}) {
    _text = text;
    _status = status;
    return this;
  }

  AppwriteResponse json(Map<String, dynamic> json, {int status = 200}) {
    _text = json;
    _status = status;
    return this;
  }
}
Drake
28 Mar, 2023, 20:28

Womp. It still might be possible with a lot of hacking...

Ipsoka
28 Mar, 2023, 20:38

Yep, got it to work with this fancy hacky method:

T? castOrNull<T>(dynamic x) => x is T ? x : null;

Ipsoka
28 Mar, 2023, 20:42
TypeScript
  AppwriteRequest? req = castOrNull<AppwriteRequest>(requ);
  AppwriteResponse? res = castOrNull<AppwriteResponse>(resp);
Ipsoka
28 Mar, 2023, 21:17

Nvm, doesnt work :/

Drake
28 Mar, 2023, 21:42

πŸ˜• ya...it might not be worth it to try and do that at the moment

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