Skip to content

Instantly share code, notes, and snippets.

@mariopepe
Created January 23, 2022 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariopepe/fcc2f12266476752dd3dd4f59ea0699c to your computer and use it in GitHub Desktop.
Save mariopepe/fcc2f12266476752dd3dd4f59ea0699c to your computer and use it in GitHub Desktop.
posts_repository_impl.dart
import 'package:dartz/dartz.dart';
import 'package:http/http.dart' as http;
import 'package:functional_error_handling/core/error_handling/error_handling.dart';
import 'package:functional_error_handling/features/post/data/datasources/json_placeholder_v1.dart';
import 'package:functional_error_handling/features/post/domain/entities/post_entity.dart';
import 'package:functional_error_handling/features/post/domain/repositories/posts_repository.dart';
class PostsRepositoryImpl implements PostsRepository {
@override
Future<Either<FailureEntity, List<PostEntity>>> fetchPosts() async {
final jsonPlaceHolderV1 = JsonPlaceholderV1(
httpClient: http.Client(),
);
try {
// We attempt to reach the API endpoint and parse the result.
final postsList = await jsonPlaceHolderV1.fetchPosts();
// I successful we return on the Right side the posts list.
return Right(postsList);
// Otherwise based on the different Exceptions we defined, we return
// failure objects on the Left side.
} on ServerException {
return const Left(ServerFailure());
} on DataParsingException {
return const Left(DataParsingFailure());
} on NoConnectionException {
return const Left(NoConnectionFailure());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment