Skip to content

Instantly share code, notes, and snippets.

View ltOgt's full-sized avatar
👁️‍🗨️

:o: ltOgt

👁️‍🗨️
View GitHub Profile
@ltOgt
ltOgt / maybeFuture.dart
Last active August 2, 2023 15:50
check if future
import 'dart:async';
typedef _Future = Future Function();
typedef _Void = void Function();
typedef _FutureOrVoid = FutureOr<void> Function();
main () {
_FutureOrVoid f;
f = () async {};
@ltOgt
ltOgt / command_and_model.dart
Last active October 25, 2022 07:36
Template for using command pattern with ChangeNotifier models
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
/// ========================================================= base command file
/// See [SetupWidget].
class BaseCommand {
static BuildContext? _rootContext;
static BuildContext get rootContext {
assert(
@ltOgt
ltOgt / datetime_key_cross_platform.dart
Created September 9, 2022 19:06
Used DateTime iso strings as keys and had some issues when moving between platforms
//MAC: 2022-08-03T21:26:45.749
//WEB: 2022-08-03T21:24:00.175
//DATA: 2022-04-14T18:00:49.290978
//LINUX: 2022-08-03T21:29:15.553488
print(DateTime.now().toIso8601String());
print(DateTime.parse("2022-08-03T21:29:15.553488").toIso8601String());
@ltOgt
ltOgt / copyWithFunction.dart
Created September 7, 2022 08:59
CopyWith that works for "resetting" nullable fields
class Foo {
int bar;
int? baz;
Foo(this.bar, this.baz);
// The "normal" copyWith does not allow to "reset" nullable
// value to `null`.
// Passing `null` means "keep the value"
Foo copyWith({
class MyMetaData {
// Whatever you want
}
// In the hit test target, e.g. drag target
return MyParentTree(
child: MetaData(
metaData: MyMetaData(...),
child: MyChildTree(),
),
@ltOgt
ltOgt / dart-const.md
Created December 27, 2021 09:56
dart const

Using const constructors inside const constructors does not work

const MyClass(int x, y) : _offset = Offset(x,y);

Does not work because the call context is not currently propagated into the constructor body. Creating MyClass with const is different than creating it with new, but we can't pass that to Offset. Putting const before Offset is invalid in the new context, and having (implicit) new is invalid in const context. This might be changed in the future, and I saw there a quite a number of tickets with the tag enhanced-const. The intuitive behaviour would be that Offset is const when MyClass is const.

@ltOgt
ltOgt / grid.dart
Created April 10, 2021 11:40
Non-Scrollable GridView-ish Widget
class Grid extends StatelessWidget {
const Grid({
Key? key,
required this.children,
this.columns,
this.rows,
this.space = 10,
this.mainAxisAlignment = MainAxisAlignment.start,
this.crossAxisAlignment = CrossAxisAlignment.center,
}) : assert(
void main() {
dynamic t = TC();
print(ifHasMethodElse(() => t.code, ""));
}
ifHasMethodElse(Function closureWithCall, dynamic elseThis) {
try {
return closureWithCall();
} catch (NoSuchMethodError, e) {
return elseThis;
void main() {
dynamic r1 = CustomErrorHandler<dynamic>().execute(
mightThrow: () => []..elementAt(2),
onError: (e) => print("Found Error: <$e>"),
shouldRethrow: false,
);
print(r1);
dynamic r2 = CustomErrorHandler<int>().execute(
mightThrow: () => 1 ~/ 0,
extension ForEachIndexedExtension<E> on List<E> {
/// Returns a new eagerly computed [List] with elements of type [T] that are created by
/// calling `f` on each element of this `List` with elements of type [E] in order of increasing index.
///
/// `f` exposes the index and the element at that index.
///
/// ___________
/// For example:
///
/// ```