Skip to content

Instantly share code, notes, and snippets.

View tsinis's full-sized avatar

Roman Cinis tsinis

View GitHub Profile
@tsinis
tsinis / main.dart
Created August 11, 2025 20:58
Extension types (transparent vs opaque)
void main() {
const invalidRating = -0.2; // Valid number, but not valid rating :(.
const rating = Rating.valid(0.2); // Error on `Rating.valid(-1)`, it's negative.
final validRating = Rating(invalidRating); // Will be clamped to 0.
final list = [rating, validRating];
print('Runtime type of list: ${list.runtimeType}'); // Prints "List<num>".
final readOnlyList = ReadOnlyList(list);
final unmodifiableList = List<num>.unmodifiable(list);
print('UnmodifiableList length: ${unmodifiableList.length}'); // Prints "2".
@tsinis
tsinis / main.dart
Created August 7, 2025 15:58
toInt vs round
void main() {
// Positive numbers
print(3.2.toInt()); // 3 (truncates decimal)
print(3.2.round()); // 3 (rounds to nearest)
print(3.7.toInt()); // 3 (truncates decimal)
print(3.7.round()); // 4 (rounds to nearest)
print(3.5.toInt()); // 3 (truncates decimal)
print(3.5.round()); // 4 (rounds away from zero)
@tsinis
tsinis / main.dart
Last active February 23, 2025 16:50
WidgetOrElse Demo
import "package:flutter/material.dart";
class WidgetOrElse<T extends Object> extends StatelessWidget {
const WidgetOrElse(
this.value,
this._builder, {
this.orElse = const Offstage(),
super.key,
});
@tsinis
tsinis / main.dart
Created October 23, 2024 14:12
Chips Summart
import "dart:math";
import "package:flutter/material.dart";
void main() => runApp(const Main());
class Main extends StatefulWidget {
const Main({super.key});
@override
@tsinis
tsinis / main.dart
Created August 27, 2024 06:56
toString() overrides
class BadPerson {
const BadPerson(
this.id, {
required this.firstName,
required this.lastName,
this.email,
this.age,
});
final int id;
@tsinis
tsinis / main.dart
Last active September 28, 2024 21:33
BLoC Notify Example
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
mixin StateNotifierMixin<State extends Object> on BlocBase<State> {
/// Emits short, notification-like state.
///
/// - `notifyDelay`: By default, one event-loop tick `Duration.zero` is used.
@tsinis
tsinis / main.dart
Last active February 29, 2024 20:00
Extend sub classes
import 'package:flutter/widgets.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) => WidgetsApp(
builder: (_, __) => const MyWidget(),
@tsinis
tsinis / main.dart
Last active July 16, 2023 14:50
Compile time constants access
// --- Regular Way ---
const constantValue = 100; // Have to remember exact name (no IDE help) :(
class _Constant {
// A little bit better...
static const value = 100; // But what about final instance = _Constant();?
}
// --- Smarter Way ---
@tsinis
tsinis / instructions.md
Last active May 27, 2023 14:01
Sharing Dart (Workshop) instructions with Github Gist

Sharing Dart (Workshop) instructions with Github Gist

To share Dartpad links for instructions you can use Github Gist with rich markdown support (basically Dartpad Workshop directly from the Gist). This trick eliminates the need for a server, deploys, or repositories, and it also works with "secret gists". Here are the steps to follow:

  1. Go to Github Gist.
  2. Create a new Gist and provide a description.
  3. Add your markdown instructions as an instructions.md file, for example:
## Instructions
@tsinis
tsinis / main.dart
Last active April 27, 2023 14:55
Power of the assert statements in Dart
import 'package:meta/meta.dart';
@immutable
class Currency {
const Currency({
required this.iso4217code,
required this.name,
this.disambiguateSymbol,
this.priority = 100,
this.smallestDenomination = 1,