Dart Cheat Sheet for TypeScript Developers
Welcome to the "Dart Cheat Sheet for TypeScript Developers". This article is meant to be a quick reference guide for software developers already familiar with TypeScript who are looking to expand their expertise into Dart.
Variables and Constants
// TypeScript
var myVar: string = "hello world";
let myLet: string = "hello world";
const myConst:string = "hello world";
// Dart
var myVar = "hello world"; // Inferred type
const String myConst = "hello world"; // Compile-time constant
final String myFinal = "hello world"; // Set the value at run-time once
late String myLateVar; // Set the value later
async/await
// TypeScript
async myFunction (myArg: string): Promise<void> {
// await something
}
// Dart
Future<void> myFunction (String myArg) async {
// await something
}
Console.log / Print
// TypeScript
console.log("hello world");
// Dart
print("hello world");
String interpolation
// TypeScript
const welcomeText = `Hello ${firstName}`;
// Dart
final welcomeText = 'Hello $firstName';
setTimeout
// TypeScript
const myTimeout = setTimeout(() => {
// do something
}, 1000);
clearTimeout(myTimeout);
// Dart
import 'dart:async';
...
final myTimeout = Timer(Duration(seconds: 1), () {
// do something
});
myTimeout.cancel();
setInterval
// TypeScript
const myInterval = setInterval(() => {
// do something
}, 1000);
clearInterval(myInterval);
// Dart
import 'dart:async';
...
final myInterval = Timer.periodic(Duration(seconds: 1), (timer) {
// do something
});
myInterval.cancel();
Comments