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 / hacker_news_scraper.dart
Created December 17, 2018 12:17
Code snippet for "Write your first Web Scraper in Dart" Medium post
// lib/hacker_news_scraper.dart
import 'dart:convert';
import 'package:http/http.dart';
import 'package:html/parser.dart';
import 'package:html/dom.dart';
Future initiate() async {
// Make API call to Hackernews homepage
var client = Client();
@graphicbeacon
graphicbeacon / virtual_dom.dart
Created December 6, 2018 22:05
A lightweight Virtual DOM implementation in Dart
import 'dart:html';
void main() {
var vApp = createElement('div',
attrs: {
'id': 'app',
},
children: [
'Hello Cat!',
createElement('img',
@graphicbeacon
graphicbeacon / async-with-futures.dart
Created November 22, 2018 14:14
Sample code demonstrating Asynchronous programming with Futures
import 'dart:convert';
import 'dart:html';
import 'dart:async';
main() {
// Basic example
var result = Future(() => 'Hello World!');
print(result);
result.then((data) => print(data));
@graphicbeacon
graphicbeacon / interfaces-and-mixins.dart
Last active November 21, 2018 13:59
Sample code demonstration Interfaces and Mixins in Dart
void main() {
var pixel = Phone('Pixel XL', 'Google');
pixel.getDeviceInfo();
pixel.getAllFeatures();
}
class FeaturesMixin {
bool blueTooth = true;
bool dualSim = false;
bool NFC = true;
@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());
@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 / 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 / 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 / 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 / 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>