Skip to content

[ Dart - Flutter ] A ValueNotifier implementation that periodically updates its value by fetching a new value from an asynchronous source enabling effortless management of real-time data. 🏔️

License

Notifications You must be signed in to change notification settings

Yodart/cyclical-value-notifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cyclical Value Notifier

Pub Version GitHub

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.

Key Features

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

Getting Started

  1. Installation: Add the cyclical_value_notifier package to your pubspec.yaml:
  dependencies:
    cyclical_value_notifier: ^1.0.0  # Replace with the latest version
  1. Import: Import the package in your Dart code
import 'package:cyclical_value_notifier/cyclical_value_notifier.dart';
  1. 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');
   },
),

Example

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');
            },
          ),
        ),
      ),
    );
  }
}

About

[ Dart - Flutter ] A ValueNotifier implementation that periodically updates its value by fetching a new value from an asynchronous source enabling effortless management of real-time data. 🏔️

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages