Back

Problem with functions in dart

  • 0
  • Self Hosted
  • Functions
  • General
usamacude
9 Sep, 2023, 13:01

I am using appwrite 1.3.7 , when writing functions in dart , the function not executing "print" commands inside "if" statements or after".then" or in functions other than "start" function

TL;DR
The user is experiencing issues with functions in Dart, specifically with the `res.json()` function returning before the `then` callbacks are executed. They are advised to read up on how futures work and to await the `listDocuments` call. Additionally, the user shares a code snippet and mentions that `print` commands are not executing properly in certain parts of the code. The user is using Appwrite 1.3.7 framework.
Drake
9 Sep, 2023, 17:24

your function might be returning before those then callbacks are being executed. it would help if you shared your actual code

usamacude
10 Sep, 2023, 05:11
TypeScript
import 'dart:convert';
import 'package:dart_appwrite/dart_appwrite.dart';
import 'car_model.dart';

Future<String?> isAdvertiserExists({required String userId, required Databases db}) async {
  String? res;
  try {
    db.listDocuments(
        databaseId: "VipcarDatabaseV3",
        collectionId: "Advertiser",
        queries: [
          Query.equal('user_id', [userId]),
        ]).then((adv){
          if(adv.documents.isNotEmpty){
            res= adv.documents[0].$id;
            print("Document id : ${adv.documents[0].$id}");
          }else{
            print("Document not found");
            res = null;
          }
        });
  } catch (e) {
    print(e.toString());
    print("isAdvertiserExists function error");
  }
  return res;
}

Future<void> start(final req, final res) async {
  

  final client = Client();
  final database = Databases(client);

  if (req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null ||
      req.variables['APPWRITE_FUNCTION_PROJECT_ID'] == null ||
      req.variables['APPWRITE_FUNCTION_API_KEY'] == null) {
    print(
        "Environment variables are not set. Function cannot use Appwrite SDK.");
  } else {
    client
        .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
        .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
        .setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
        .setSelfSigned(status: true);
  }

  final String appUserId = req.variables["APPWRITE_FUNCTION_USER_ID"];
  bool resultCode = true;
  String message = "ok";
  bool? adverOk;
  late CarObject car;

  try {
    car = CarObject.fromJson(jsonDecode(req.payload));
    print(car.carCategoryId);
  } catch (e) {
    print(e.toString());
  }
  car.advertiserId = appUserId;
  isAdvertiserExists(userId: appUserId, db: database).then((res) {
    if (res != null) {
      adverOk = true;
    } else {
      adverOk = false;
    }
  });

  res.json({
    "code": resultCode,
    "message": message,
    "attr": adverOk,
  });
}
usamacude
10 Sep, 2023, 05:23

In this function i am receiving data from user then define model for that data, then trying to find the document that has user id as attribute.

Drake
10 Sep, 2023, 06:24

You have to await the list documents call

Drake
10 Sep, 2023, 06:26

You should probably read into how futures work some more too to get a better understanding of how they work

Drake
10 Sep, 2023, 06:26

In short, the res.json() is called and your function returns before any of the thens get executed

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