Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active February 13, 2024 12:51
Show Gist options
  • Save PlugFox/86e19f52bb6606b39032dd32f87855af to your computer and use it in GitHub Desktop.
Save PlugFox/86e19f52bb6606b39032dd32f87855af to your computer and use it in GitHub Desktop.
Вопросы вопросики, каверзные и не очень (не для собеседований)
void main() => Future<void>(() async {
final stream = Stream<int>.fromIterable([1, 2, 3]).map<String>((v) {
print(v);
return v.toString();
}).asBroadcastStream();
await for (final _ in stream) {
print('+');
}
await for (final _ in stream) {
print('-');
}
});
void main() {
final a = A()..v = 1;
final b = A()..v = 2;
print(a.v);
print(b.v);
}
class A {
int? _v;
int get v => _v ??= 0;
set v(int value) => _v ??= value;
static final A _instance = A._internal();
factory A() => _instance;
A._internal();
}
import 'dart:async';
void main() async {
try {
print('1');
await Completer().future;
print('2');
} on Object {
print('3');
} finally {
print('4');
}
}
void main() {
eq(A(1), A(2));
eq(A(1), A(1));
eq(A(1), const A(1));
eq(A.f(1), A.f(1));
eq(const A(1), const A(1));
eq(const A(1), const A(2));
eq(const A(1), const A.f(1));
eq(const A.f(1), const A.f(1));
}
void eq(Object a, Object b) =>
print(identical(a, b));
class A {
final int value;
const A(this.value);
const factory A.f(int value) = A._;
const A._(this.value);
@override
int get hashCode => value;
@override
bool operator ==(Object obj) =>
obj is A && obj.value == value;
}
import 'package:flutter/widgets.dart';
void main() => runApp(App());
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 70,
minHeight: 70,
maxWidth: 150,
maxHeight: 150,
),
child: Container(
color: const Color.fromARGB(255, 255, 0, 0), // RED
width: 50,
height: 50,
),
);
}
import 'dart:async';
void main() {
Future<void>.delayed(const Duration(seconds: 1), () => log(1));
Future<void>.delayed(Duration.zero, () => log(2));
Future<void>(() => log(3)).then<void>((_) => log(4));
Future<void>.sync(() => log(5));
Future<void>.microtask(() => log(6));
log(7);
Future<void>.microtask(() => log(8));
Future<void>.sync(() => log(9));
Future<void>(() => log(10));
Future<void>.delayed(Duration.zero, () => log(11));
Future<void>.delayed(const Duration(seconds: 1), () => log(12));
}
void log(int i) => print(i.toString());
import 'dart:async';
void main() {
Future<void>(() => print('1'));
scheduleMicrotask(() => print('2'));
Future<void>.sync(() => print('3')).then<void>((_) => print('4'));
print('5');
}
void main() =>
print(f());
int f() {
try {
try {
return 1;
} finally {
return 2;
}
} finally {
return 3;
}
}
void main() {
var value = .0;
while (value < 1) {
value += .1;
}
print(value == 1);
print(value == 1.0);
print(1.0 == 1);
}
import 'dart:async';
Future<void> main() async {
try {
await someLongFunction().timeout(const Duration(milliseconds: 350));
return;
} on TimeoutException {
print('TimeoutException');
return;
}
}
Future<void> someLongFunction() async {
final periodic = Stream<int>.periodic(
const Duration(milliseconds: 250),
(i) => i,
);
await for (final i in periodic.take(3)) {
print(i);
}
}
void main() {
final a = A();
print(identical(a.fn, a.fn));
print(identical(A().fn, A().fn));
print(identical(() {}, () {}));
print(identical(A.st, A.st));
}
class A {
static st() {}
void fn() {}
}
void main() {
print('BEGIN');
try {
gen().map<int>((i) {
print(' * map $i');
return i;
});
return;
} on Object catch (_) {
print('ERROR');
} finally {
print('END');
}
}
Iterable<int> gen() sync* {
for (int i = 0; i < 3; i++) {
yield i;
print(' * emit $i');
}
}
void main() =>
Stream<void>.fromIterable(gen())
.first
.then<void>(print);
Iterable<int> gen() sync* {
for (int i = 0; i < 3; i++) {
yield i;
print(' * emit $i');
}
}
void main() => Future<void>(() async {
final a = await genAsync().first;
print('gotcha a = $a');
final b = genSync().first;
print('gotcha b = $b');
});
Stream<int> genAsync() async* {
for (var i = 0; i < 10; i++) {
yield i;
print('async emit i = $i');
}
}
Iterable<int> genSync() sync* {
for (var j = 0; j < 10; j++) {
yield j;
print('sync emit j = $j');
}
}
void main() {
print('Non nulluble:');
A<int>();
print('\nNulluble:');
A<int?>();
print('\nUnderstanding:');
int? value;
print('int? value is bool?: ${(value is bool?)}');
}
class A<T> {
A() {
print('$runtimeType is <bool>: ${this is A<bool>}');
print('$runtimeType is <bool?>: ${this is A<bool?>}');
print('$runtimeType is <int>: ${this is A<int>}');
print('$runtimeType is <int?>: ${this is A<int?>}');
}
}
final int a = () {
print(2);
return 2;
}();
void main() {
print(1);
a;
print(3);
a;
}
void main() {
late String a = '1';
String f() {
a = '2';
print('3');
return '4';
}
late String b = f();
print(a);
print(b);
}
void main() => B().log();
abstract class A {
void log() {
print('A');
}
}
class B extends A with C, D {
@override
void log() {
print('B');
super.log();
}
}
mixin C on A {
@override
void log() {
print('C');
super.log();
}
}
mixin D on A {
@override
void log() {
print('D');
super.log();
}
}
void main() {
const int value1 = 1;
const double value2 = 1.0;
final buffer = StringBuffer()
..writeln('Integer:')
..writeln(value1 is double)
..writeln(value1 is int)
..writeln(value1 is num)
..writeln()
..writeln('Real:')
..writeln(value2 is double)
..writeln(value2 is int)
..writeln(value2 is num)
..writeln()
..writeln('Equality:')
..writeln(value1 == value2)
..writeln(identical(value1, value2));
print(buffer.toString());
}
import 'dart:async';
void main() {
print(1);
runZonedGuarded(() {
try {
Future(() {
print(2);
throw UnsupportedError('Some error');
print(3);
});
} on Object {
print(4);
} finally {
print(5);
}
}, (_, __) {
print(6);
});
print(7);
}
// @dart=2.12
void main() {
final a = null;
final b = 1;
print(a is Object);
print(b is Object);
print(a is dynamic);
print(b is dynamic);
print(a is Null);
print(b is num);
}
// @dart=2.9
void main() {
final a = null;
final b = 1;
print(a is Object);
print(b is Object);
print(a is dynamic);
print(b is dynamic);
print(a is Null);
print(b is num);
}
// @dart=2.12
void main() {
bool? value;
final String result = getType(value);
print(result);
}
/// Get JSON value and return its type representation
String getType(Object? value) {
if (value is Map<String, Object?>?) {
return 'object';
} else if (value is Iterable<Object?>) {
return 'array';
} else if (value is num?) {
return 'number';
} else if (value is bool?) {
return 'boolean';
} else if (value is String?) {
return 'text';
} else {
throw UnsupportedError('null');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment