mainstream 0.3.0 copy "mainstream: ^0.3.0" to clipboard
mainstream: ^0.3.0 copied to clipboard

A StreamBuilder alternative that provides builder and event callbacks.

example/lib/main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mainstream/mainstream.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

final myStream = Stream<int>.periodic(Duration(seconds: 1), (x) => (x == 3) ? throw Exception('oops') : x).take(5);

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: MainStream<int>(
          stream: myStream,
          onData: (data) => print(data),
          onError: (error) => _showAlert(context, error.toString()),
          onDone: () => print('Done!'),
          busyBuilder: (_) => CircularProgressIndicator(),
          dataBuilder: (_, data) => Text(data.toString()),
          errorBuilder: (_, error) => Text(error.toString()),
        ),
      ),
    );
  }

  void _showAlert(BuildContext context, String text) {
    showDialog(
      context: context,
      builder: (_) {
        return AlertDialog(
          content: Text(text),
          actions: <Widget>[
            TextButton(
              child: Text('OK'),
              onPressed: () => Navigator.of(context).pop(),
            )
          ],
        );
      },
    );
  }
}
14
likes
150
points
23
downloads

Publisher

verified publishermartinrybak.com

Weekly Downloads

A StreamBuilder alternative that provides builder and event callbacks.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on mainstream