Skip to content

Instantly share code, notes, and snippets.

View s0nerik's full-sized avatar
🇺🇦

Sasha Isaienko s0nerik

🇺🇦
View GitHub Profile
@s0nerik
s0nerik / main.dart
Created October 25, 2023 12:26
Counter example
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@s0nerik
s0nerik / main.dart
Last active September 27, 2023 09:07
(Dart) FutureOr handling differences
import 'dart:async';
/// Produces the following output (since [futureOrResult] value is available synchronously):
/// ```
/// process: 0
/// result: 0
/// process: 1
/// ```
Future<void> main() async {
unawaited(process());
@s0nerik
s0nerik / main.dart
Created September 21, 2022 10:27
amber-fauna-4028
void main() {
print(DateTime.now());
print(DateTime.now().toUtc());
print(DateTime.now().difference(DateTime.now().toUtc()));
}
@s0nerik
s0nerik / main.dart
Last active October 28, 2021 21:17
Dart: Future<void> vs void execution
import 'dart:async';
void main() {
fn1();
scheduleMicrotask(() {
print('microtask after fn1');
});
fn2();
scheduleMicrotask(() {
print('microtask after fn2');
@s0nerik
s0nerik / main.dart
Created October 21, 2021 09:59
UseCase specification comparison (class vs function, explicit vs implicit deps)
void main() {
final di = DIContainer();
final quarterExpenses1 = di.getComputeQuarterExpensesUseCase().perform(100000);
print(quarterExpenses1);
final quarterExpenses2 = di.getComputeQuarterExpensesUseCaseFunction()(100000);
print(quarterExpenses2);
final quarterExpenses3 = di.getExplicitComputeQuarterExpensesUseCaseFunction()(100000);
print(quarterExpenses3);
}
@s0nerik
s0nerik / main.dart
Created October 18, 2021 11:23
Dart async error catch example (CORRECT, actually captures the expection)
Future<void> main() async {
await test();
}
Future<void> test() async {
try {
return await errorGenerator();
} catch (e) {
print('!!!CAPTURED ERROR!!!');
} finally {
@s0nerik
s0nerik / main.dart
Created October 18, 2021 11:23
Dart async error catch example (WRONG, doesn't capture expection)
Future<void> main() async {
await test();
}
Future<void> test() async {
try {
return errorGenerator();
} catch (e) {
print('!!!CAPTURED ERROR!!!');
} finally {
@s0nerik
s0nerik / main.dart
Last active October 4, 2021 12:25
Figma line height vs Flutter's TextStyle height illustration
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
const lineHeight = 27.0;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
@s0nerik
s0nerik / enum_mapper.dart
Last active July 17, 2021 13:12
Two way enum mapper
typedef EnumMapperCallback<T, E> = T Function(E value);
class EnumMapper<E, T> {
EnumMapper(List<E> enumValues, EnumMapperCallback<T, E> mapper) {
for (final enumValue in enumValues) {
final key = enumValue;
final value = mapper(enumValue);
_forwardMap[key] = value;
_inverseMap[value] = key;
}
@s0nerik
s0nerik / cluster.dart
Last active November 12, 2020 10:03
Flutter cluster bitmap rendering example
BitmapDescriptor _stationPin;
ui.Image _clusterBgImage;
Future<BitmapDescriptor> _getClusterBitmap(
BuildContext context,
MapCluster cluster,
) async {
if (!cluster.isCluster && _stationPin != null) {
return _stationPin;
}