Skip to content

Instantly share code, notes, and snippets.

View cdmunoz's full-sized avatar

Carlos Daniel cdmunoz

View GitHub Profile
/// Stub that holds all of the values in memory
class StubStorage implements Storage {
final hash = <String, dynamic>{};
@override
bool? getBool(String key) => hash[key] as bool?;
@override
String? getString(String key) => hash[key] as String?;
Response<T> buildFailureResponse<T>(
String message, {
int code = 400,
http.BaseRequest? request,
}) {
final body = jsonDecode(message) as Map<String, dynamic>;
final error = buildBackendError(statusCode: code, body: body);
return Response(
http.Response(message, code, request: request),
Response<T> buildFailureResponse<T>(
String message, {
int code = 400,
http.BaseRequest? request,
}) {
final body = jsonDecode(message) as Map<String, dynamic>;
final error = buildBackendError(statusCode: code, body: body);
return Response(
http.Response(message, code, request: request),
Response<T> buildFailureResponse<T>(
String message, {
int code = 400,
http.BaseRequest? request,
}) {
final body = jsonDecode(message) as Map<String, dynamic>;
final error = buildBackendError(statusCode: code, body: body);
return Response(
http.Response(message, code, request: request),
group('with a success', () {
setUpAll(() async {
when(pushNotifier.requestPermissions()).thenSucceed(true);
when(pushNotifier.onTokenRefresh()).thenStream(fcmRefreshedToken);
await subject().onInit();
});
test('ask for push permissions', () {
verify(pushNotifier.requestPermissions());
/// mocking the PostExpectation with a Future
extension FutureMock<T> on PostExpectation<Future<T>> {
void thenFuture(T body) {
thenAnswer((realInvocation) => Future.value(body));
}
}
/// we use Result objet similar than a monad which returns a success, a failure, etc
/// this extension mocks a Result of success, failure or in order
await onErrorRetry(
doIt: () async {
await _twilioModule.setup();
_analytic.track('Platform Loaded');
_router.replaceTo(HomeRoute());
status = status.rebuild((b) => b..isLoadingVisible = false);
},
typedef FunctionOnRetry = Function(
dynamic ex,
int retryCount,
int retryInMilliseconds,
);
Future<void> onErrorRetry({
required Future Function() doIt,
required FunctionOnRetry onRetry,
required int maxRetries,
/// this class uses Isolates.run, only available after Dart 2.19, allowing in parallel obtaining a file in bytes
/// then answering to its caller without blocking the UI
/// remember: when using isolates, the functions they call must be top level or static
import 'dart:isolate';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
Future<http.Response> _fileResponse(Uri uri) async {
/// this is part of a stateful widget, that uses Isolates.compute to do a heavy task, in this case download and
/// display an image from internet in parallel.
/// this will help to improve performance for our scenario when a chat messages screen should display all messages
class _MessagesImageState extends State<MessagesImage> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
// onTap logic