uigitdev_stream_holder 0.0.1 copy "uigitdev_stream_holder: ^0.0.1" to clipboard
uigitdev_stream_holder: ^0.0.1 copied to clipboard

Stream and data holder class, which helps to ensure reusability and reduce the number of lines of code.

example/main.dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uigitdev_stream_holder/src/stream_holder.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<MainProvider>(create: (_) => MainProvider()),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SizedBox(
          width: double.maxFinite,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              _countDataBuilder(),
              _countButton(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _countButton() {
    return Consumer<MainProvider>(
      builder: (context, provider, _) {
        return TextButton(
          onPressed: () => provider.countStreamHolder.addData(Random().nextInt(500)),
          child: Text('+'),
        );
      },
    );
  }

  Widget _countDataBuilder() {
    return Consumer<MainProvider>(
      builder: (context, provider, _) {
        return StreamBuilder<int>(
          stream: provider.countStreamHolder.stream,
          initialData: provider.countStreamHolder.data,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Text(snapshot.data!.toString());
            } else if (snapshot.hasError) {
              return Text(snapshot.error.toString());
            } else {
              return Text('Loading');
            }
          },
        );
      },
    );
  }
}

class MainProvider extends ChangeNotifier {
  final countStreamHolder = StreamHolder<int>(0);
}
4
likes
130
pub points
30%
popularity

Publisher

verified publisheruigitdev.com

Stream and data holder class, which helps to ensure reusability and reduce the number of lines of code.

Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (LICENSE)

Dependencies

flutter, provider

More

Packages that depend on uigitdev_stream_holder