Dart Requires Chilkat v11.4.0+
Dart
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------
final sbScript = CkStringBuilder();
try {
sbScript.loadFile('js_function_returning_array.js', 'utf-8');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
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 each function
final funcCall = CkJsonObject();
// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.
funcCall.updateString('name', 'getDays');
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());
// Output:
// {
// "type": "array",
// "value": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ]
// }
// Access each array value..
var count = result.sizeOfArray('value');
var i = 0;
while (i < count) {
result.i = i;
print(result.stringOf('value[i]'));
i++;
}
// ------------------------------------------------------------------------------
// Call the getRange(start,end) function
funcCall.clear();
funcCall.updateString('name', 'getRange');
funcCall.updateInt('args[0]', 14);
funcCall.updateInt('args[1]', 21);
js.callFunction(funcCall, result);
print(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21
// ]
// }
count = result.sizeOfArray('value');
i = 0;
while (i < count) {
result.i = i;
print(result.intOf('value[i]'));
i++;
}
// ------------------------------------------------------------------------------
// Call the getEmployees() function
funcCall.clear();
funcCall.updateString('name', 'getEmployees');
js.callFunction(funcCall, result);
print(result.emit());
// Output:
// {
// "type": "array",
// "value": [
// {
// "id": 101,
// "name": "Alice",
// "role": "Dev"
// },
// {
// "id": 102,
// "name": "Bob",
// "role": "Manager"
// }
// ]
// }
count = result.sizeOfArray('value');
i = 0;
while (i < count) {
result.i = i;
print('name: ${result.stringOf('value[i].name')}');
print('role: ${result.stringOf('value[i].role')}');
print('id: ${result.intOf('value[i].id')}');
print('');
i++;
}
}
JavaScript
function getDays() {
return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}
function getRange(start, end) {
var arr = [];
for (var i = start; i <= end; i++) {
arr.push(i);
}
return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]
function getEmployees() {
return [
{ id: 101, name: "Alice", role: "Dev" },
{ id: 102, name: "Bob", role: "Manager" }
];
}