flutter_shared_utilities 1.0.1
flutter_shared_utilities: ^1.0.1 copied to clipboard
A comprehensive Flutter utilities package with extensions, models, and utility functions.
flutter_shared_utilities #
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 'package:flutter_shared_utilities/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 'package:flutter_shared_utilities/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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - 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.
π Links #
Made with β€οΈ for the Flutter community