Skip to content

Instantly share code, notes, and snippets.

View graphicbeacon's full-sized avatar
🏠
Working from home

Jermaine Oppong graphicbeacon

🏠
Working from home
View GitHub Profile
@graphicbeacon
graphicbeacon / books_controller.dart
Created June 15, 2018 16:03
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (6)
// ..
class BooksController extends HTTPController {
@httpGet
Future<Response> getAllBooks() async {
var query = new Query<Book>()..join(set: (book) => book.authors);
return new Response.ok(await query.fetch());
}
// ..
// ..
}
@graphicbeacon
graphicbeacon / books_controller.dart
Last active June 15, 2018 16:08
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (7)
// ..
class BooksController extends HTTPController {
//..
@httpPost
Future<Response> addBook(@HTTPBody() Book book) async {
var query = new Query<Book>()..values = book;
var insertedBook = await query.insert();
// Insert authors from payload
await Future.forEach(book.authors, (Author a) async {
@graphicbeacon
graphicbeacon / variables-and-inbuilt-types.dart
Last active June 28, 2018 14:34
Variables and Built-in types in Dart
void main() {
// Variables and constants
var edition = 2;
int year = 2011;
const language = 'Dart';
String name; // == null
// Number
var num1 = 5;
var num2 = 10.0;
@graphicbeacon
graphicbeacon / letter-animation-effect.dart
Last active June 28, 2018 14:35
Letter animation effect
import 'dart:async';
void main() {
List<String> alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
var stream = new Stream.fromIterable(alphabets);
var count = 1;
stream.listen((char) {
Timer(Duration(seconds: 1 * count), () {
print(char);
@graphicbeacon
graphicbeacon / top_10_array_utilities.dart
Last active June 28, 2018 16:31
Sample snippet for "Top 10 Array utility methods you should know (Dart) 🎯" article on Medium (Full solution)
void main() {
// forEach()
var fruits = ['banana', 'pineapple', 'watermelon'];
fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon
print('\n---\n');
// map()
var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList();
@graphicbeacon
graphicbeacon / index.html
Created July 6, 2018 23:11
Code for a timer
<span class="clock"></span>
@graphicbeacon
graphicbeacon / top_10_map_utilties.dart
Created July 6, 2018 23:25
Sample code for "Top 10 Map/Object utility methods you should know (Dart) 🎯" (Complete solution)
void main() {
var user = {
"firstName": "Tom",
"age": 25,
};
// 1. addAll()
user.addAll({
"lastName": "Smith",
"age": 26,
@graphicbeacon
graphicbeacon / shelf_basic_server.dart
Created July 12, 2018 18:14
Sample code for "Deploying Dart 2 apps to Heroku" on Medium
import 'dart:io' show Platform;
import 'dart:async' show runZoned;
import 'package:path/path.dart' show join, dirname;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';
void main() {
// Assumes the server lives in bin/ and that `webdev build` ran
var pathToBuild = join(dirname(Platform.script.toFilePath()), '..', 'build');
@graphicbeacon
graphicbeacon / top_10_string_utilities.dart
Last active August 8, 2018 21:58
Sample code for "Top 10 String utility methods you should know (Dart) 🎯" (Complete solution)
void main() async {
var str1 = 'Lorem';
var str2 = '$str1 ipsum'; // String interpolation
var str3 = '''Multi
Line
$str1 $str2'''; // Multi line string
print(str1.contains('rem')); // true
print(str2.startsWith('Lorem')); // true
@graphicbeacon
graphicbeacon / main.dart
Created September 13, 2018 19:27
Sample snippet for "Top 8 Date methods you should know (Dart)" on Dev.to
void main() {
var now = new DateTime.now();
var berlinWallFell = new DateTime.utc(1989, 11, 9);
var moonLanding = DateTime.parse('1969-07-20 20:18:04Z');
print(now);
print(berlinWallFell);
print(moonLanding);
print('\n---\n');