os_basecode 0.0.4 os_basecode: ^0.0.4 copied to clipboard
A Core Package That Must Have on every flutter project if using Clean Code Architecture
Basic Functionality #
Failur Class #
Use Failure Class On Reporitory Implement for Return On Left Type Either
- ServerFailure
- ConnectionFailure
- CommonFailure
- DatabaseFailure
Examples:
Future<Either<Failure, List<PriceInfoEntity>>> fetchAllGroup() async {
try {
var result = await alertRemoteDataSource.fetchAllGroup();
return Right(result.map((e) => e.toEntity()).toList());
} on DatabaseException catch (e) {
return Left(DatabaseFailure(e.message));
} on ServerException {
return const Left(ServerFailure("Harga Emas Hari Ini Gagal Dimuat"));
} catch (e) {
return const Left(CommonFailure("Harga Emas Hari Ini Gagal Dimuat"));
}
}
Exception Class #
Use Exception Class On Datasource for Throw, and then catch the throw on Repository Implement
- ServerException
- DatabaseException
Examples:
Future<List<PriceInfo>> fetchAllGroup() async {
try {
Dio dio = Dio();
var result = await dio.get(priceInfoURL);
return priceInfoFromJson(result.data);
} on DioError {
throw ServerException();
} catch (e) {
throw DatabaseException();
}
}
Client Class #
Client Class is Customized DIO implementation that can be used, its customable properties like baseURL, headers, showLogging
Example:
Initialize a simple client, without any headers:
var client = Client(baseURL: "your_base_url", showLogging: true)..init();
Use client on your data_source: assume we already inject the client using dependencies injection
Class GroupRemoteDataSourceImpl implement GroupRemoteDataSource {
Dio dio;
GroupRemoteDataSourceImpl(this.client);
Future<List<PriceInfo>> fetchAllGroup() async {
try {
var result = await dio.get(priceInfoURL);
return priceInfoFromJson(result.data);
} on DioError {
throw ServerException();
} catch (e) {
throw DatabaseException();
}
}
}
Utility Methods #
debounce #
use debounce on bloc transformer Example:
on<OnQueryBanner>((event, emit) async {
// Your BLoC Code here
}, transformer: debounce(const Duration(milliseconds: 500)));
List Installed Packages Inside #
bloc: ^8.1.2
dartz: ^0.10.1
dio: ^5.1.2
equatable: ^2.0.5
flutter_animate: ^4.1.1+1
flutter_bloc: ^8.1.3
flutter_screenutil: ^5.8.2
font_awesome_flutter: ^10.4.0
get_it: ^7.6.0
go_router: ^7.1.1
google_fonts: ^4.0.5
lottie: ^2.3.2
path_provider: ^2.0.15
rxdart: ^0.27.7
shared_preferences: ^2.1.1
shimmer: ^3.0.0
supercharged: ^2.1.1