Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.4.0+

Call a JavaScript Function Passing an Object Argument

See more JavaScript Examples

Demonstrates how to call a JavaScript function with an argument that is an object.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This is the JavaScript function we'll call:

  // function describeCar(car) {
  // 	console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
  // }

  final sbScript = CkStringBuilder();
  sbScript.append('function describeCar(car) { console.log(`This is a \${car.year} \${car.make} \${car.model}.`); }');

  final js = CkJs();

  final result = CkJsonObject();
  result.emitCompact = false;

  // Call Eval to add the function to the context's global object
  try {
    js.eval(sbScript, result);
  } on ChilkatException catch (e) {
    // Examine the result for an exception.
    print(result.emit());

    // Also examine the LastErrorText.
    print(e.lastErrorText);
    return;
  }

  // ------------------------------------------------------------------------------
  // Call the function describeCar(car)

  final funcCall = CkJsonObject();
  funcCall.emitCompact = false;

  // Create JSON specifying the function name and arguments
  // In this case, there is only 1 argument, and it is an object.

  // {
  //   "name": "describeCar",
  //   "args": [
  //     {
  //       "make": "Toyota",
  //       "model": "Corolla",
  //       "year": 2022
  //     }
  //   ]
  // }

  funcCall.updateString('name', 'describeCar');

  // Create the JSON object that is the argument.
  final arg = CkJsonObject();
  arg.updateString('make', 'Toyota');
  arg.updateString('model', 'Corolla');
  arg.updateInt('year', 2022);

  // Create the arguments array.
  final argsArray = CkJsonArray();
  argsArray.addObjectCopyAt(0, arg);

  // Add the "args" array to the funcCall.
  funcCall.appendArrayCopy('args', argsArray);

  print(funcCall.emit());

  try {
    js.callFunction(funcCall, result);
  } on ChilkatException catch (e) {
    // Examine the result for an exception.
    print(result.emit());

    // Also examine the LastErrorText.
    print(e.lastErrorText);
    return;
  }

  print(result.emit());

  // The describeCar JavaScript function returns nothing. 
  // Therefore, the result is "undefined".

  // {
  //   "type": "undefined",
  //   "value": "undefined"
  // }

  // However, the function emitted text to the console.

  final sbOut = CkStringBuilder();
  js.consoleOutputSb(sbOut);
  print(sbOut.getAsString());

  // Output:
  // This is a 2022 Toyota Corolla.

  // -----------------------------------------------------------
  // Note: If the object argument is simple, this is an alternative
  // and simpler way of creating the funcCall:

  funcCall.clear();
  funcCall.updateString('name', 'describeCar');
  funcCall.updateString('args[0].make', 'Toyota');
  funcCall.updateString('args[0].model', 'Corolla');
  funcCall.updateInt('args[0].year', 2022);
  print(funcCall.emit());
}