Skip to content

Instantly share code, notes, and snippets.

View evaisse's full-sized avatar

Emmanuel Vaïsse evaisse

  • none
  • Nancy, France
View GitHub Profile
@evaisse
evaisse / main.dart
Last active June 30, 2025 13:59
Describing usage of context.select/read/watch etc
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
/// Represents a city using a record, with its name (including flag emoji),
/// population, and seaside status.
/// Records provide automatic equality and hash code implementation.
typedef City = ({String name, int population, bool isSeaside});
/// Manages the selection of a city and provides available city data.
///
@evaisse
evaisse / main.dart
Last active June 30, 2025 13:19
ChangeNotifier with provider
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
/// Data model for the counter, extending ChangeNotifier to notify listeners of changes.
class CounterData extends ValueNotifier<int> {
CounterData() : super(0);
void increment() => value = value + 1;
}
@evaisse
evaisse / doc_returns_types_for_function.dart
Last active June 19, 2025 14:13
doc_returns_types_for_function.dart
/// Attention à TOUJOURS typer le retour des callback/builders
/// que vous allez passer aux widgets/controllers.
class Foo {
Function()? onFoo;
void Function()? onFoufou;
Foo({ this.onFoo, this.onFoufou });
}
void main() {
@evaisse
evaisse / copywith_nullable.dart
Last active June 5, 2025 14:14
copyWith with nullable and optional
/// General demonstration about the annoying way to set to null a copy of another object
///
/// @see https://github.com/dart-lang/language/issues/877
/// @see https://stackoverflow.com/questions/68009392/dart-custom-copywith-method-with-nullable-properties
class Person {
final String name;
final String? nickName;
Person({required this.name, this.nickName});
@evaisse
evaisse / pointy_castle_asymmetric_encryption.dart
Created March 11, 2025 09:12
Asymmetric Encryption RSA-AES-CBC with public/private keys using pointycastle lib
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
import 'package:basic_utils/basic_utils.dart';
import 'package:encrypt/encrypt.dart';
// ignore: depend_on_referenced_packages
import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart' as flutter;
import 'package:pointycastle/export.dart';
@evaisse
evaisse / main.dart
Created February 11, 2025 15:20
Algrebraic data types
// Définition d'une classe scellée pour représenter un résultat
// Sealed class : https://dart.dev/language/class-modifiers#sealed
sealed class ASealedClass {}
// cas 1
class Success extends ASealedClass {}
// cas 2
class Failure extends ASealedClass {}
/// sous la forme d'un enum
enum AnEnum { success, error }
@evaisse
evaisse / the_problem_with_dynamic.dart
Created September 13, 2024 14:17
The problem with `dynamic` in dart
void main() {
dynamic myDynamicValue = null;
myDynamicValue.length;
Object? myTypedValue = myDynamicValue;
myTypedValue.length; // that's helpful
@evaisse
evaisse / typed_generics.dart
Last active September 13, 2024 12:01
Show how extends at least `Object` allow to forbid passing dynamic generics
class Generic<T> {
final T that;
Generic(this.that);
}
class TypedGeneric<T extends Object> {
final T that;
TypedGeneric(this.that);
}
@evaisse
evaisse / castor_safe_try_as.dart
Last active September 13, 2024 10:32
cast0r — Safe try/as and transform extension that works on any object for Dart
/// Cast Or provide default value (optional or safely defaulted)
/// by the way, "castor" is the french word for beaver...
/// ```
/// ___
/// /. .\
/// =\_t_/=
/// [|]
/// ```
///
extension CastOrExtension<T extends Object> on T {
class Cool {
final int count;
Cool({ int? count = 20 }): this.count = count ?? 20;
}
class NotCool {
final int count;
NotCool({ this.count = 20 });
}