Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Last active February 22, 2017 20:15
Show Gist options
  • Save matanlurey/a7974eaba2e3219084f8e908f208f4e2 to your computer and use it in GitHub Desktop.
Save matanlurey/a7974eaba2e3219084f8e908f208f4e2 to your computer and use it in GitHub Desktop.
An example of a JavaScript library that fetches your contacts from an "API".
@JS()
library fetch_contacts_interop;
import 'package:func/func.dart';
import 'package:js/js.dart';
// Natively calls "fetch_contacts", but we can use typed Dart code while developing!
@JS('fetch_contacts')
external void fetchContacts(String prefix, VoidFunc1<List<String>> callback);
// Example use
void main() {
fetchContacts('A', (results) => 'Contacts: $results'));
}
// An example of a JavaScript library that fetches your contacts from an "API".
//
// Example use:
// fetch_contacts('C', (contacts) => { ... });
const contacts = [
'Amy',
'Beth',
'Carl',
'Dave'
];
function fetch_contacts(prefix, callback) {
let results = contacts.filter((contact) => {
return contact.startsWith(prefix);
});
setTimeout(() => {
callback(results);
}, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment