#[SOLVED] dart & flutter - cloud function execution error from client and server

151 messages · Page 1 of 1 (latest)

sly elbow
#
src/server.dart:115:25: Error: Method not found: 'main'.
              user_code.main(context)
                        ^^^^
src/server.dart:125:38: Error: Method not found: 'main'.
            output = await user_code.main(context);
                                     ^^^^
Error: AOT compilation failed
Generating AOT kernel dill failed!```


Am unable to detect what the error is
Kindly assist if you can
#

dart - function build error

#

dart - function deployment error

teal tundra
#

Can I see your code? Did you start from a starter template?

#

It's trying to run the main function of your Appwrite function, but isn't finding the main function

sly elbow
#
  final client = Client();

  // Uncomment the services you need, delete the ones you don't
  // final account = Account(client);
  // final avatars = Avatars(client);
  // final database = Databases(client);
  // final functions = Functions(client);
  // final health = Health(client);
  // final locale = Locale(client);
  // final storage = Storage(client);
  // final teams = Teams(client);
  // final users = Users(client);

  client
      .setEndpoint("https://cloud.appwrite.io/v1")
      .setProject(projectId)
      .setKey("API_KEY")
      .setSelfSigned(status: true);
  emptier();
  setter();
  // api from env var
  apiToken = (req.variables['DAMMY_GONGOZ_API']);
  // decode user request
  userRequest = await jsonDecode(req.payload);

  // decide which service
  superResponse = await whichServiceExec();
  {
    res.json({
      'Status': superResponse,
    });
  }
}```
teal tundra
#

this is very very old

sly elbow
teal tundra
#

the CLI

#

can u do appwrite -v ?

#

what version is ur cli

#

maybe we missed something 🙃

sly elbow
#

2.0.2

teal tundra
#

!

sly elbow
teal tundra
#

we're on like

#

4.1

#

😅

#

try updating the CLI

#

lemme know how that goes

sly elbow
#

what's the CMD for upgrade?

teal tundra
#

How'd u install?

sly elbow
#

I think via npm I guess

teal tundra
#

$ npm install -g appwrite-cli

#

run this again

#

i think should work

#

🤣

#

idk npm that well

sly elbow
teal tundra
#

uninstall and try fresh install?

#

i don't remember the npm command 🤔 lemme go google 😄

sly elbow
#

Upgraded

teal tundra
#

IF it's still giving you an old function lmk

#

I'll go chase down a fix 😄

sly elbow
#

the new pattern seems trickier

sly elbow
#

So how do I decode a payload from client?

teal tundra
#

body is already decoded

#

bodyRaw is a string

#

You can check a few examples here

sly elbow
#

does it mean that data sent from client is not encoded?
or do you mean that the data gotten by the function is automatically decoded when it gets to it?

#

```// The req object contains the request data
if (context.req.method == 'GET') {
// Send a response with the res object helpers
// res.send() dispatches a string back to the client
return context.res.send("Hello);
}

// res.json() is a handy helper for sending JSON
return context.res.json({
'motto': 'Build like a team of hundreds_',
'learn': 'https://appwrite.io/docs',
'connect': 'https://appwrite.io/discord',
'getInspired': 'https://builtwith.appwrite.io',
});```

sly elbow
#

@teal tundra

sly elbow
#

@proper fiber
@runic sandal
@real storm
@plucky jasper

thi may be inappropriate to tag you all, but I had no option
been unable to execute dart function after cli upgrade to latest...

proper fiber
#

That's the issue

sly elbow
#

I have upgraded i @proper fiber
to version 4

proper fiber
#

What's the updated code?

sly elbow
#
  //
  final client = Client();
  client
      .setEndpoint("https://cloud.appwrite.io/v1")
      .setProject(projectId)
      .setKey("API_KEY")
      .setSelfSigned(status: true);
  // You can log messages to the console
  context.log('Hello, Logs!');

  // If something goes wrong, log an error
   context.error('Hello, Errors!');
  emptier();
  userRequest = context.req.query["numberToProcess"];
setter();

  // api from env var
   apiToken = (context.variables['DAMMY_GONGOZ_API']);
  // decode user request

  // decide which service
  superResponse = await whichServiceExec();
  ///////////////////////////////////////////////////
  // The `req` object contains the request data
if (context.req.method == 'GET') {
// Send a response with the res object helpers
 //  `res.send()` dispatches a string back to the client
   return context.res.send(superResponse);
   }

  // `res.json()` is a handy helper for sending JSON
  return context.res.json({
    'motto': 'Build like a team of hundreds_',
    'learn': 'https://appwrite.io/docs',
    'connect': 'https://appwrite.io/discord',
    'getInspired': 'https://builtwith.appwrite.io',
    'num': userRequest.toString()
  });
}```
#

Client Code Was:

          .createExecution(functionId: 'all_services', data: jsonEncode(data), );```

Changed To(after seeing new docs):

```  dynamic execution = await appWriteLogicExtender.functions.createExecution(
          functionId: 'all_services',
          body: json.encode(data),
          async: false,
          path: '/',
          method: 'GET',
          headers: {'X-Custom-Header': '123'});```
#

@proper fiber

sly elbow
runic sandal
sly elbow
runic sandal
teal tundra
teal tundra
#

body is automatically decoded to be JSON
bodyRaw gives you the raw HTTP request body

sly elbow
sly elbow
#

or maybe if I could see a working client code that sends request and cloud function receiving it and making use of it before sending a response back to client

I have nto been abl to proceed for day now

runic sandal
sly elbow
runic sandal
sly elbow
sly elbow
#

Now that I am trying to deploy the function the old way, I am still getting errors.
Old function error during deployment after it failed, below is the logs:

src/server.dart:115:25: Error: Method not found: 'main'.
              user_code.main(context)
                        ^^^^
src/server.dart:125:38: Error: Method not found: 'main'.
            output = await user_code.main(context);
                                     ^^^^
Error: AOT compilation failed
Generating AOT kernel dill failed!```

Sincerely, the developer experience with the updated dart function is not friendly to me.
I can't use either new way of deploying or the old way.

I am just delayed by 'no idea of what to do' for days.
teal tundra
#

It works for me

#

I think there's something wrong somewhere, I'm curious why it's not working, too.

timid leaf
#

What version of dart are you using?
Have you set the function entrypoint?

runic sandal
runic sandal
#

and you're on appwrite cloud right?

sly elbow
sly elbow
sly elbow
runic sandal
runic sandal
sly elbow
sly elbow
runic sandal
sly elbow
sly elbow
runic sandal
sly elbow
#

even when I changed to the new syntax, it won't even send any communication to the cloud function

sly elbow
#

appwrite: ^11.0.0

is the only needed package for flutter front end?

sly elbow
#

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

runic sandal
sly elbow
#

Appwrite Class in client code

class AppWriteLogic extends GetxController {
//  create account, function instance
  Databases databases = Databases(client);
  Functions functions = Functions(client);
  Realtime realtime = Realtime(client);
  late Account account;
// Functions functions = Functions(client);
  @override
  void onInit() async {
    if (kDebugMode) {
      print('appwrite controller inited');
    }
    super.onInit();
    appwriteEndpointProjectID();
  }

  String walletBalanceCollectionID = "walletBalanceCollectionID";
  String walletDatabaseID = "walletDatabaseID";
//   pass in secret data
  appwriteEndpointProjectID() {
    client.setEndpoint(endpoint); // Your API Endpoint
    client.setProject(projectId); // ID
    client.setSelfSigned(status: false);
    account = Account(client);
  }
}

The use of Appwrite Init Class in client code
AppWriteLogic appWriteLogicExtender = AppWriteLogic();
The use of execution api in client code

 dynamic data = {"name":userName, "class":userClass}
 Future execution = appWriteLogicExtender.functions.createExecution(
        functionId: 'automate_service_sender_2',
        data: jsonEncode(data),
      );
      execution.then((response) async {
      // do this
      print(response);
      }).catchError((error) {
       print(error);
      });

Dart function in server code

Future<dynamic> main(final context) async {
final client = Client();
  client
       .setEndpoint("https://cloud.appwrite.io/v1")
       .setProject(projectID)
       .setKey("API_KEY")
       .setSelfSigned(status: true);

  userRequest = jsonDecode(context.req.body);
  context.res.json({"To": "Hi"});
  context.res.send(userRequest.toString());
}
#

dart & flutter - cloud function execution error from client and server

sly elbow
#

@runic sandal @proper fiber @timid leaf
here is the code above

runic sandal
runic sandal
sly elbow
#
Receiver: Instance of 'Functions'
Tried calling: createExecution(data: "{\"numberToProcess\":\"phoneNum\",\"userMail\":\"[email protected]\",\"userID\":\"65383643cc5e7399b65b9\",\"totalServiceCharge\":\"120\",\"serciceTypeAndService\":\"MTN SME\",\"serviceQuantity\":\"500MB\",\"walletBalanceDocumentID\":\"phoneNum\",\"networkID\":\"1\",\"planID\":\"289\",\"serviceName\":\"Mobile Data\",\"customerAmount\":\"120\",\"currentUserBalance\":\"170\",\"previousWalletBalance\":\"50\",\"whatTypeOfTransaction\":\"Mobile Data\",\"supplierRate\":\"107.5\",\"transactionID\":\"unique()\"}", functionId: "all_services", headers: _Map len:1, method: "POST", path: "/", xasync: false)
Found: createExecution({required String functionId, String? data, bool? xasync}) => Future<Execution>```

the above error was getting using the new function...
```dynamic execution = await appWriteLogicExtender.functions.createExecution(
        functionId: 'automate_service_sender_2',
        data: jsonEncode(data),
        xasync: false,
        path: '/',
        method: 'POST',
        headers: {
          'content-type': 'application/json',
        },
  );```
runic sandal
sly elbow
#
 Future execution = appWriteLogicExtender.functions.createExecution(
        functionId: 'automate_service_sender_2',
        data: jsonEncode(data),
      );
      execution.then((response) async {
      // do this
      print(response);
      }).catchError((error) {
       print(error);
      });```
sly elbow
runic sandal
runic sandal
sly elbow
sly elbow
runic sandal
runic sandal
sly elbow
sly elbow
runic sandal
sly elbow
#

or how do I switch to former version, to avoid all this stress

runic sandal
#

Try stopping your debugger, cleaning your flutter project, and restarting your IDE

sly elbow
#

What do you suggest for me to do?

#

Cause when I even removed the initialization, it was still same, so I just left the object use

runic sandal
sly elbow
runic sandal
sly elbow
#

the former syntax for getting variables was
req.variables['ENV_VAR'];

what is the new syntax?
@runic sandal

runic sandal
sly elbow
sly elbow
runic sandal
sly elbow
sly elbow
#
}```

is this accurate?
#

My laptop isn't charged, waiting for power supply and by then you may be busy, so if I can get the clarity as soon as you can assist

runic sandal
#

I'm closing this post now since the original problem is solved. Feel free to create a new post if you're still having trouble.

sly elbow
#

So how do I get and decode what the client said inside the dart function?

To be used by the cloud function during execution lifecycle?

sly elbow
runic sandal
#

[SOLVED] dart & flutter - cloud function execution error from client and server

sly elbow
#

[closed] dart & flutter - cloud function execution error from client and server

runic sandal
#

[SOLVED] dart & flutter - cloud function execution error from client and server