Back

Does the Function in Appwrite Dart, will return a response?

  • 0
  • Flutter
  • Functions
Mosh Ontong
8 Oct, 2023, 06:13

In my cloud function I have this context.res.json({..}). Now in Dart how to get the json response? in Flutter

TL;DR
The user is asking for help with a Dart function in the Appwrite framework. They provide some code snippets and explain that they are not getting the expected response. They also mention a specific user they would like assistance from. The solution is not provided in the thread.
Drake
8 Oct, 2023, 06:13

Create a sync execution and then the result should have it

Mosh Ontong
8 Oct, 2023, 06:14
TypeScript
 final execution = await functions.createExecution(
        functionId: functionId,
        body: jsonEncode(mapTracking.toAppwriteDocument()),
        xasync: false,
        path: '/',
        method: 'POST',
        headers: {
          'content-type': 'application/json',
        },
      );

set the xasync into false?

Drake
9 Oct, 2023, 19:12

correct

dammy
24 Oct, 2023, 18:21

hello, @Mosh Ontong can you please share the code entry and decoding the user request.

I just saw how you sent user response using context.res.json({..})

Mosh Ontong
24 Oct, 2023, 23:18

In my dart function:

TypeScript
Future<dynamic> main(final context) async {
  final client = Client();

  final database = Databases(client);
  final users = Users(client);

  if (Platform.environment['APPWRITE_FUNCTION_ENDPOINT'] == null ||
      Platform.environment['APPWRITE_FUNCTION_API_KEY'] == null) {
    context.error(
        "Environment variables are not set. Function cannot use Appwrite SDK.");
  } else {  
    client
        .setEndpoint(Platform.environment['APPWRITE_FUNCTION_ENDPOINT']!)
        .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'])
        .setKey(Platform.environment['APPWRITE_FUNCTION_API_KEY']);
  }

  if (context.req.method == 'POST') {
    final payload = context.req.body as Map<String, dynamic>;
    ..... 
}}
Mosh Ontong
24 Oct, 2023, 23:21

And this is how I execute the function and get the response

TypeScript

      final execution = await functions.createExecution(
        functionId: functionId,
        body: jsonEncode(
          {
            'documentId': serviceId,
            'ratings': totalRating,
          },
        ),
        xasync: false,
        path: '/',
        method: 'POST',
        headers: {
          'content-type': 'application/json',
        },
      );
      _logger.info('Successfully created bookTrace: $execution');

      final response =
          jsonDecode(execution.responseBody) as Map<String, dynamic>;
dammy
25 Oct, 2023, 10:23

thanks @Mosh Ontong

dammy
25 Oct, 2023, 10:37

I still get client side error

dammy
25 Oct, 2023, 10:58

@Mosh Ontong Please will you be available for a 5 mins virtual assistance, a getting so many things wrong

dammy
25 Oct, 2023, 13:48

please someone should help, I ave been on this for days

Binyamin
25 Oct, 2023, 15:49

What is your code & error?

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