os_basecode 0.1.9 copy "os_basecode: ^0.1.9" to clipboard
os_basecode: ^0.1.9 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("Your message here"));
    } on CommonException{
        return const Left(CommonFailure("Your message here"));
    } catch (e) {
      return const Left(DatabaseFailure("Your message here"));
    }
  }

Exception Class #

Use Exception Class On Datasource for Throw, and then catch the throw on Repository Implement

  • ServerException
  • DatabaseException
  • CommonException

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();
      //common exception
      throw CommonException();
    }
  }

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, headers: {}, handleWhenUnauthorized: (){
  //Handle your 401 exception here
})..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)));

NumericTextFormatter #

use NumericTextFormatter inside Textformfiel inputFormatter Example:

return TextFormField(
        controller: nominalController,
        inputFormatters: [
          FilteringTextInputFormatter.digitsOnly,
          NumericTextFormatter()   // <----- ADD HERE
        ],
        keyboardType: TextInputType.number,
);

formatRupiah #

use formatRupiah to convert number to string currency Example:

var nominal = 1000
var formatted = formatRupiah(nominal);
print(formatted) // Rp. 10.000

getNumberOnly #

use getNumberOnly for convert string currency to number

var formatted = "Rp. 10.000"
var number = getNumberOnly(formatted);
print(number) // 10000

List Installed Packages Inside #

bloc: ^8.1.2
dartz: ^0.10.1
dio: ^5.2.0
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: ^8.0.1
google_fonts: ^5.0.0
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
device_preview: ^1.1.0
intl: ^0.18.0

License #

MIT License

OctaStudio.ID 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:

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

  2. 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.

OctaStudio.ID - https://www.octastudio.id