polling_builder 0.1.0 copy "polling_builder: ^0.1.0" to clipboard
polling_builder: ^0.1.0 copied to clipboard

A Flutter widget that polls data at regular intervals and when conditions are met, similar to StreamBuilder but for polling APIs.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:polling_builder/polling_builder.dart';

void main() => runApp(PollingBuilderExample());

class PollingBuilderExample extends StatelessWidget {
  const PollingBuilderExample({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('Polling Example')),
      body: PollingBuilder<Map<String, dynamic>>(
        dataFetcher: () async {
          // Your API call here
          // Simulate network delay
          await Future.delayed(Duration(milliseconds: 800));

          // Get current timestamp and generate a random value
          final now = DateTime.now();
          final value = now.second;

          // Return mock data
          return {
            'timestamp': now.toIso8601String(),
            'value': value,
          };
        },
        pollInterval: Duration(seconds: 1),
        buildWhen: (previous, current) {
          // Custom rebuild logic
          return previous?['timestamp'] != current?['timestamp'];
        },
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(child: Text('Error: ${snapshot.error}'));
          }

          if (!snapshot.hasData) {
            return Center(child: CircularProgressIndicator());
          }

          final data = snapshot.data!;
          return Center(
            child: Text('Latest data: ${data['value']}'),
          );
        },
      ),
    ));
  }
}
3
likes
160
points
21
downloads

Publisher

verified publishermulderdigital.com

Weekly Downloads

A Flutter widget that polls data at regular intervals and when conditions are met, similar to StreamBuilder but for polling APIs.

Repository (GitHub)
View/report issues

Topics

#widget #polling #api #stream #data-fetching

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on polling_builder