Responsive Ad Code Here

Header Ads

Stream in flutter

why use Stream flutter. when use statefull widgets when app performance low so use stateless widgets. but when the data show in app when use in statefull widgets other wise use stream this example and the stream ui change every time .example the firebase remote config when use the stream the main workm change ui. other wise firebase remote config time not change ui ok . more chat app , weather app ,  collaboration app like google docs


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

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

  @override
  Widget build(BuildContext context) {
    final TextEditingController textEditingController = TextEditingController();
    List language = [];
    StreamController streamController = StreamController();

    return Scaffold(
      appBar: AppBar(
        title: const Text('Language'),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 5),
        child: Column(
          children: [
            TextFormField(
              controller: textEditingController,
            ),
            SizedBox(
              width: 150,
              child: ElevatedButton(
                onPressed: () {
                  final input = textEditingController.text;
                  language.add(input);
                  streamController.sink.add(language);
                  textEditingController.clear();
                },
                child: const Text('New Lenguage'),
              ),
            ),
            Expanded(
              child: Container(
                color: Colors.green,
                child: StreamBuilder(
                  stream: streamController.stream,
                  builder: (context, snapShot) {
                    if (snapShot.hasData) {
                      return ListView.builder(
                          itemCount: snapShot.data.length,
                          itemBuilder: (c, index) {
                            return Card(
                              child: ListTile(
                                title: Text(
                                  snapShot.data[index],
                                ),
                              ),
                            );
                          });
                    } else {
                      return const Text('null');
                    }
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}


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

  @override
  Widget build(BuildContext context) {
    int counter = 0;
    StreamController<int> streamController = StreamController<int>();
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          counter++;
          streamController.sink.add(counter);
        },
        child: const Icon(Icons.add),
      ),
      body: SafeArea(
        child: Center(
          child: StreamBuilder(
            stream: streamController.stream,
            builder: (v, snapShot) {
              if (snapShot.hasData) {
                return Text(snapShot.data.toString());
              } else {
                return const Text('O');
              }
            },
          ),
        ),
      ),
    );
  }
}