dart_patterns 0.3.2 copy "dart_patterns: ^0.3.2" to clipboard
dart_patterns: ^0.3.2 copied to clipboard

Collection of useful patterns for Dart

Dart patterns #

Collection of useful patterns for Dart

GitHub stars Twitter Follow

Documentation #

Result #

final r = R.guard(() {
  final v = random.nextInt(2);
  if (v == 0) throw Exception('error');
  return v;
})

switch (r) {
  case Success(value: final v):
    print(v);
  case Failure(error: final e, stackTrace: final stackTrace):
    print(e);
    print(stackTrace);
}

LateResult #

final r = Late.fromStream(() async* {
  await Future.delayed(Duration(seconds: 1));
  final v = random.nextInt(2);
  if (v == 0) throw Exception('error');
  yield v;
})

r.forEach((r) {
  switch (r) {
    case Pending:
      print('no values recieved yet');
    case Success(value: final v):
      print(v);
    case Failure(error: final e, stackTrace: final stackTrace):
      print(e);
      print(stackTrace);
  }
});

AsyncResult #

final r = await Async.asStream(() async {
  await Future.delayed(Duration(seconds: 1));
  final v = random.nextInt(2);
  if (v == 0) throw Exception('error');
  return v;
});

stream.forEach((r) {
  switch (r) {
    case Loading:
      print('loading');
    case Success(value: final v):
      print(v);
    case Failure(error: final e, stackTrace: final stackTrace):
      print(e);
      print(stackTrace);
  }
});

LateAsyncResult #

final client = HttpClient();

final r = (() async* {
  yield LateAsync.pending();

  try {
    final req = await client.openUrl('GET', Uri.parse('https://example.com'));
    yield LateAsync.loading();

    final res = await req.close();
    final contentLength = res.contentLength;

    var received = 0;
    var sb = StringBuffer();

    await for (final chunk in res.transform(Utf8Decoder())) {
      received += chunk.length;
      yield LateAsync.loading(received / contentLength);
      sb.write(chunk);
    }

    yield LateAsync.success(sb.toString());
  } catch (e, stackTrace) {
    yield LateAsync.failure(e, stackTrace);
  }
})()

r.forEach((r) {
  switch (r) {
    case Pending:
      print('connection not established yet');
    case Loading(progress: final progress):
      print('loading: $progress');
    case Success(value: final v):
      print(v);
    case Failure(error: final e, stackTrace: final stackTrace):
      print(e);
      print(stackTrace);
  }
});

Experimental #

LICENSE #

Copyright 2023 Andrei Lesnitsky

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.