Assorted Dart code snippets

2019-01-15

Dart Code Snippets

Dart, the language behind Flutter, is a useful and powerful language. Here are some snippets I found useful at some stage.

Simple 'Hello, World!' style code to test your environment

Simple and easy to copy and paste to just do a quick environment check.

void main() {
    print('Hello, Dart!');
}

Obtaining the current date and testing if it is the same day as a given date

Figures out whether a particular set of times are on the same day.

void main() {
    var now = DateTime.now();
    var sameDay = now.subtract(Duration(hours: 1));
    print("The times are the same day: ${isSameDay(now, sameDay)}");
}

isSameDay(DateTime d1, DateTime d2) {
    return (d1.day == d2.day && d1.month == d2.month && d1.year == d2.year);
}



Return to the blog index