flutter_shared_utilities 1.0.2 copy "flutter_shared_utilities: ^1.0.2" to clipboard
flutter_shared_utilities: ^1.0.2 copied to clipboard

A comprehensive Flutter utilities package with extensions, models, and utility functions.

flutter_shared_utilities #

pub package License: MIT Flutter

A comprehensive Flutter utilities package that provides essential extensions, models, and utility functions to streamline your Flutter development workflow. This package includes safe parsing utilities, string manipulation extensions, list operations, and more.

✨ Features #

  • πŸ”§ Safe Parsing: Robust JSON parsing with error handling
  • πŸ“ String Extensions: Case-insensitive comparisons, JSON validation, and utility methods
  • πŸ“‹ List Utilities: Smart list operations with duplicate prevention
  • 🎨 Color Extensions: Enhanced color manipulation utilities
  • πŸ—ΊοΈ Map Extensions: Safe map parsing and utility functions
  • πŸ“Š Base Data Models: Abstract classes for consistent data handling
  • πŸ”Œ Interfaces: Logger and connectivity service interfaces
  • πŸ›‘οΈ Type Safety: Full null safety support with comprehensive type checking

πŸ“¦ Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_shared_utilities: ^1.0.1

Then run:

flutter pub get

πŸš€ Quick Start #

import '../../flutter_shared_utilities.dart';

void main() {
  // String utilities
  String? text = "Hello World";
  bool isNullEmpty = text.isNullEmpty; // false
  bool startsWith = text.startsWithIgnoreCase("hello"); // true

  // Safe JSON parsing
  String jsonString = '{"name": "John", "age": 30}';
  final parsed = SafeParser.safeDecodeJson(jsonString);

  // List utilities
  List<String> fruits = ['apple', 'banana'];
  fruits.addAllIfNotExists(['apple', 'orange']); // Only adds 'orange'
}

πŸ“š API Reference #

String Extensions #

StringUtilExtensions

Provides utility methods for string manipulation and validation:

String? text = "Hello World";

// Case-insensitive operations
bool equals = text.compareWithoutCase("hello world"); // true
bool startsWith = text.startsWithIgnoreCase("hello"); // true
bool contains = text.containsWithoutCase("world"); // true

// Null/empty checks
bool isNull = text.isNullString; // false
bool isEmpty = text.isNullEmpty; // false

// JSON validation
bool isObject = '{"key": "value"}'.isJsonObject; // true
bool isArray = '[1, 2, 3]'.isJsonArray; // true
bool isPrimitive = '"string"'.isJsonPrimitive; // true

List Extensions #

ListUtilExtensions

Smart list operations with duplicate prevention:

List<String> items = ['apple', 'banana'];

// Insert if not exists
bool inserted = items.insertIfNotExists('apple', index: 0); // false (already exists)
bool inserted2 = items.insertIfNotExists('orange', index: 1); // true

// Add all if not exists
items.addAllIfNotExists(['apple', 'grape', 'banana']); // Only adds 'grape'

// Remove if exists
bool removed = items.removeIfExist('apple'); // true

Safe Parser #

SafeParser

Robust JSON parsing with error handling:

// Safe JSON encoding
String encoded = SafeParser.safeEncodeJson({'name': 'John', 'age': 30});

// Safe JSON decoding
Object? decoded = SafeParser.safeDecodeJson('{"name": "John", "age": 30}');

// Parse iterable with type safety
List<dynamic> data = [1, 2, 3, 'invalid', 4];
Iterable<int> numbers = SafeParser.parseIterable<int>(data); // [1, 2, 3, 4]

Base Data Model #

BaseDataModel

Abstract class for consistent data model implementation:

class User extends BaseDataModel<User> {
  final String name;
  final int age;

  const User({required this.name, required this.age});

  @override
  User fromMap(Map<String, dynamic> map) {
    return User(
      name: map['name'] as String,
      age: map['age'] as int,
    );
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'age': age,
    };
  }

  @override
  List<Object?> get props => [name, age];
}

// Usage
final user = User(name: 'John', age: 30);
String json = user.toJson();
User fromJson = user.fromJson(json);

Interfaces #

AppLogger

Interface for application logging:

abstract interface class AppLogger {
  void setDebugMode(bool value);
  void setTrace(bool value);
  void d(String message, {String? tag, Object? error, StackTrace? stackTrace});
  void i(String message, {String? tag, Object? error, StackTrace? stackTrace});
  void w(String message, {String? tag, Object? error, StackTrace? stackTrace});
  void e(String message, {required StackTrace stackTrace, required Object? error, String? tag});
  void t(String message, {String? tag, Object? error, StackTrace? stackTrace});
}

ConnectivityService

Interface for connectivity management:

abstract interface class ConnectivityService {
  Stream<bool> get connectivityStream;
  Future<bool> get isConnected;
  Future<void> initialize();
  void dispose();
}

πŸ”§ Additional Utilities #

Safe Debug Print #

import '../../utils/safe_debug_print.dart';

safeDebugLog('Debug message', stackTrace: StackTrace.current);

Object Serialization Extensions #

// Safe type conversion
String? value = '123';
int? number = value.fromSerializable<int>(); // 123
double? decimal = value.fromSerializable<double>(); // 123.0

πŸ§ͺ Testing #

The package includes comprehensive tests for all functionality. To run the tests:

flutter test

🀝 Contributing #

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License #

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ› Issues and Feedback #

Please file issues and feature requests on the GitHub repository.

πŸ“ˆ Version History #

See CHANGELOG.md for a detailed version history.


Made with ❀️ for the Flutter community

1
likes
0
points
581
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive Flutter utilities package with extensions, models, and utility functions.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

collection, equatable, flutter

More

Packages that depend on flutter_shared_utilities