Skip to content

Instantly share code, notes, and snippets.

View lukepighetti's full-sized avatar
🦞

Luke Pighetti lukepighetti

🦞
View GitHub Profile
@lukepighetti
lukepighetti / dart.yml
Last active June 10, 2022 07:49
Flutter Web + GitHub Actions + Firebase Hosting Preview Channels
# .github/workflows/dart.yml
name: build
on:
push:
branches: [master]
pull_request:
branches: [master]
@lukepighetti
lukepighetti / database.dart
Last active March 25, 2021 18:43
A dead simple JSON based key/value store that is unbelievably useful for scripts and tools
import 'dart:convert';
import 'dart:io';
class Database {
/// A simple JSON file database
Database(String path) : file = File(path);
final File file;
var _store = <String, dynamic>{};
@lukepighetti
lukepighetti / example.dart
Last active March 27, 2021 00:54
Simple route generator that exposes arguments and query params to route builders
main() {
BuildContext context;
final router = NavigationService(context);
return MaterialApp(
onGenerateRoute: (settings) => router.handleGenerateRoute(
settings,
routes: {
'/': (params, context) => HomeView(),
@lukepighetti
lukepighetti / json_encode.dart
Last active April 11, 2021 00:36
Testing jsonEncode with a class that has a toJson method.
import 'dart:convert';
void main() {
final foo = Foo('bar');
print(foo);
print(foo.foo);
print(jsonEncode(foo));
print(jsonEncode([foo, foo, foo]));
}
@lukepighetti
lukepighetti / main.dart
Created April 12, 2021 13:43
Using flutter_portal to create a popover that allows the base view to remain interactive
import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
static const title = 'Spike Overlay TextField';
/// An example of operations you can do with control flow collections.
///
/// They make it very easy to do map, filter, and spread operations, all
/// while retaining the base class of [List<T>], [Set<T>], and [Map<K,V>]
void main() {
final source = [1, 2, 3, null, 4, 5, null];
/// List control flow, map & filter
final myList = [
for (var number in source)
@lukepighetti
lukepighetti / Makefile
Last active May 31, 2021 17:29
Basic Flutter Makefile
VERSION = 1.0.0
BUILD = 8
CHANGELOG = Added item counts and a celebration when a list is completed.
.PHONY: clean test build distribute
clean:
flutter clean
flutter pub get
pod repo update

This is a pattern I came up with when I was working on a fairly large one person app (25k LOC, legal/regulation calculator, lots of unit testing). It is very simple and very effective.

The short version is you have a RootStore that listens to all of it's child stores. Each store has an async initState method and a dispose method. Make observer mixins for platform channel related items like App Lifecycle, Geolocation, Geofencing that add more lifecycle methods to these stores and manage their own initState/dispose.

I realize that a gist is going to look like a bit of a cluster for something of this nature but I bet if you pull it all down into a project and start playing around with it you'll really enjoy it. Especially now that provider has context.select.

Here's an example of my folder structure in lib/features as a bit of a bonus

@lukepighetti
lukepighetti / data_classes.dart
Last active May 11, 2022 11:52
possible first class data class syntax in Dart
data class User {
String name,
}
data class Manager extends User {
List<User> reports,
}
abstract class Detailed {
String get details;
@lukepighetti
lukepighetti / main.dart
Last active August 16, 2021 13:03
Second attempt at switch alternative
void main() {
final a = 4;
final truthy = {
Case(a % 2 == 0): "even",
Case(a == 3): "three",
Case(a >= 4): "four or more",
}.build(
fallback: 'less than two',
);