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 / 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 / console.dart
Last active October 27, 2023 21:16
Final snippets for "How to use JavaScript libraries in your Dart applications" article
// web/console.dart
@JS('console')
library console;
import 'package:js/js.dart';
external void log(dynamic str);
@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');
@graphicbeacon
graphicbeacon / index.html
Last active May 13, 2019 08:24
Create a countdown timer in Dart
<div>
<strong id="days"></strong>
<strong id="hours"></strong>
<strong id="minutes"></strong>
<strong id="seconds"></strong>
</div>
@graphicbeacon
graphicbeacon / main.dart
Created October 8, 2018 19:48
Solution code for "How to handle the POST request body in Dart without using a framework"
import 'dart:io';
import 'dart:convert';
void main() async {
var server = await HttpServer.bind('127.0.0.1', 9000);
await for (HttpRequest req in server) {
if (req.method == 'POST' && req.headers.contentType.toString() == 'application/x-www-form-urlencoded') {
var content = await req.transform(Utf8Decoder()).join();
var queryParams = Uri(query: content).queryParameters;
@graphicbeacon
graphicbeacon / read-and-write-files.dart
Last active November 21, 2018 11:59
Learn Dart #5: Read and write files in under 30 seconds
import 'dart:io';
main() async {
var file = File('data.txt');
var contents;
if (await file.exists()) {
// Read file
contents = await file.readAsString();
print(contents);
@graphicbeacon
graphicbeacon / chat_room.dart
Last active November 8, 2018 13:07
Code snippet from "Build a chat application in Dart 2 (Part 2)" article
// Absolute imports
import 'dart:html';
// Relative imports
import './view.dart';
class ChatRoomView implements View {
ChatRoomView(this.params) : _contents = DocumentFragment() {
onEnter();
}
@graphicbeacon
graphicbeacon / serverside-post-request.dart
Last active November 21, 2018 11:58
Learn Dart #8: Perform a Server-side POST request in under 30 seconds (Solution)
import 'dart:io';
import 'dart:convert';
main() async {
var apiUrl = Uri.parse('https://jsonplaceholder.typicode.com/posts');
var client = HttpClient(); // `new` keyword optional
// 1. Create request
HttpClientRequest request = await client.postUrl(apiUrl);
@graphicbeacon
graphicbeacon / classes-and-inheritance.dart
Last active November 21, 2018 11:58
Sample code demonstrating Classes and Inheritance in Dart
void main() {
var johnny = Person('Johnny', 42)
..speak()
..name = 'Big Johnny'
..speak();
var johnnyB = Person('Johnny', 42);
print(johnny == johnnyB);
var bob = Employee('Bob', 23, DateTime.now());