Dart

Call jQuery Functions from Dart

The standard documentation for Dart and JavaScript interoperability isn’t great. It refers to the ‘dart:js’ library which although works is a bit verbose to get things done.

For instance, to call a jQuery function, you would need

import 'dart:js';
 
...
context.callMethod(r'$', ['.some.class.selector']).callMethod('someFunction');
...

The dart team has since then released the js package with which you can simply call

import 'package:js/js.dart';
 
...
context.$('.some.class.selector').someFunction();
...

Basically native syntax. Of course you have to add it to the yaml and pub get again.

Functional Programming Example in Dart

For a quick reference.

At the time of writing (Feb 2014), the top results from a Google search on ‘Dart functional programming’ or ‘Dart list comprehension’ don’t immediately yield clear examples. That’s crazy given that Dart is such a boss language. So here’s an example:

// double all values
List myList = [1, 2, 3, 4];
List doubledList = myList.map((x) => x * 2);

You can do the standard map, reduce, filter on Iterables on Dart. Of course you can chain them up like other languages. Although it’s not strictly using the set building notation like in Python, you can still pretty much do everything so something like:

[x * 2 for x in myList if x % 2 == 0]

in Python would look like:

myList.where((x) => x % 2 == 0).map((x) => x * 2);
 Scroll to top