Counter Service
Counter Service is a Flutter package that provides a WebSocket-based counter functionality. It allows you to connect to a WebSocket server, increment or decrement a counter, and receive real-time updates.
Features
- WebSocket connection management
- Real-time counter updates
- Increment and decrement counter actions
- Singleton pattern for easy access across your app
- Error handling and connection status tracking
Getting started
To use this package, add counter_service
as a dependency in your pubspec.yaml
file:
dependencies:
counter_service: ^0.0.1
Then, run flutter pub get
to install the package.
Usage
Here's a quick example of how to use the Counter Service:
import 'package:counter_service/counter_service.dart';
void main() async {
final counterService = CounterService();
// Connect to the WebSocket server
await counterService.connect('wss://your-websocket-server.com');
// Listen for counter updates
counterService.counterStream.listen((value) {
print('Counter value: $value');
});
// Increment the counter
await counterService.incrementCounter();
// Decrement the counter
await counterService.decrementCounter();
// Get the current counter value
print('Current counter value: ${counterService.counterValue}');
// Disconnect when done
counterService.disconnect();
}
Additional information
Dependencies
This package depends on:
web_socket_channel: 2.4.5
for WebSocket communication
Error Handling
The Counter Service includes basic error handling and logging. You can extend the _onError
method in the CounterService
class to implement custom error handling or user notifications.
Reconnection
The current implementation does not automatically reconnect on disconnection. You can implement reconnection logic in the _onDisconnected
method if needed.
Contributing
Contributions to the Counter Service package are welcome. Please feel free to submit issues and pull requests on our GitHub repository.
License
This package is released under the MIT License. See the LICENSE file for details.