Cyclical Value Notifier is a Flutter package that provides a customizable ValueNotifier
implementation capable of periodic value updates from asynchronous sources. It allows you to effortlessly manage and display real-time data in your Flutter applications by periodically fetching and updating values.
- Periodic Value Updates: Fetch and update values from asynchronous sources at specified intervals.
- Customizable Fetch Function: Define your own asynchronous fetch function to retrieve the latest data.
- Effortless Real-Time Data: Keep your UI up-to-date with minimal code and maximum flexibility.
- Easy Integration: Seamlessly integrate with your existing Flutter projects.
- Installation: Add the
cyclical_value_notifier
package to yourpubspec.yaml
:
dependencies:
cyclical_value_notifier: ^1.0.0 # Replace with the latest version
- Import: Import the package in your Dart code
import 'package:cyclical_value_notifier/cyclical_value_notifier.dart';
- Usage:
final notifier = CyclicalValueNotifier<int>(
0,
interval: Duration(seconds: 1),
fetch: () async {
// Fetch the latest value from an asynchronous source
// For example, an API call to get the current count
final response = await apiService.getCount();
return response.count;
},
);
// Access the latest data from the notifier
final currentValue = notifier.value;
// Or build a widget reactive to the changes on the notifier
ValueListenableBuilder<int>(
valueListenable: notifier,
builder: (context, value, child) {
return Text('Current Value: $value');
},
),
Here's a simple example demonstrating how to use CyclicalValueNotifier to fetch and update a value periodically:
import 'package:flutter/material.dart';
import 'package:cyclical_value_notifier/cyclical_value_notifier.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final apiService = ApiService(); // Replace with your own service
@override
Widget build(BuildContext context) {
final notifier = CyclicalValueNotifier<int>(
0,
interval: Duration(seconds: 5),
fetch: () async {
// Fetch the latest value from an asynchronous source
final response = await apiService.getCount();
return response.count;
},
);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Cyclical Value Notifier Example'),
),
body: Center(
child: ValueListenableBuilder<int>(
valueListenable: notifier,
builder: (context, value, child) {
return Text('Current Value: $value');
},
),
),
),
);
}
}