The original

Medium.com/flutterdevs…

The body of the

Asynchronous interactions may need an ideal opportunity to wrap up. Occasionally, some values may be emitted before the end of the cycle. In Dart, you can create a capacity to return a Stream that emits values while the asynchronous process is active. Suppose you need to construct a widget in A Flutter based on a snapshot of a Stream. There is a widget called StreamBuilder.

In this blog, we will explore the StreamBuilder in Flutter. We will also implement a demo and show you how to use StreamBuilder in your Flutter application.

Introduction:

StreamBuilder can listen on exposed streams and return widgets and snapshots of captured stream information. The creek maker made two arguments.

A stream

Builder, which can change multiple components in a flow into widgets

A Stream is like a line. When you enter a value from one end and a listener from the other, the listener gets the value. A stream can have multiple listeners, and the payload of these listeners can gain pipelines, which will gain value. How to place values on a stream is achieved by using a flow controller. The flow builder is a widget that changes a user-defined object into a flow.

The builder:

To use the StreamBuilder, call the following constructor:

const StreamBuilder({
Key? key,
Stream<T>? stream,
T? initialData,
required AsyncWidgetBuilder<T> builder,
})
Copy the code

In effect, you need to create a Stream and pass it as a Stream contention. Then, at this point, you need to pass an AsyncWidgetBuilder that can be used to construct widgets that depend on Stream snapshots.

Parameters:

Here are some of the StreamBuilderare parameters:

  • Key? key: the widget’s key that controls how the widget is replaced by another widget
  • Stream<T>? stream: A stream whose snapshot can be taken by the generator function
  • T? initialData: The data will be used to create an initial snapshot
  • required AsyncWidgetBuilder<T> builderThe: build process is used by the generator

How to implement code in the DART file:

You need to implement it separately in your code:

Let’s create a stream:

The following function returns a Stream that generates one number per second. You need to use the async * keyword to create a stream. To emit a value, use the yield keyword followed by the value to emit.

Stream<int> generateNumbers = (() async* {
  await Future<void>.delayed(Duration(seconds: 2));

  for (int i = 1; i <= 10; i++) {
    await Future<void>.delayed(Duration(seconds: 1));
    yield i;
  }
})();
Copy the code

From that point onward, pass it as the stream argument

Starting at that point, pass it along as a stream parameter

StreamBuilder<int>(
stream: generateNumbers,
// other arguments
)
Copy the code

Let’s create an AsyncWidgetBuilder

The constructor expects you to pass a named contention builder of type AsyncWidgetBuilder. This is a function with two arguments, both of type BuildContext and AsyncSnapshot < t >. Subsequent boundaries (containing the current snapshot) can be used to determine what should be rendered.

To create this function, you first need to understand AsyncSnapshot. AsyncSnapshot is an immutable description of the latest communication using asynchronous computation. In this unique case, it resolves the latest communication with the Stream. You can get the latest snapshot of a stream using the AsyncSnapshot property. One of the properties you might want to use is connectionState, an enumeration that converts the current association state to an asynchronous calculation, which in this particular case is Steam.

StreamBuilder<int>(
  stream: generateNumbers,
  builder: (
      BuildContext context,
      AsyncSnapshot<int> snapshot,
      ) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return CircularProgressIndicator();
    } else if (snapshot.connectionState == ConnectionState.active
        || snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
        return const Text('Error');
      } else if (snapshot.hasData) {
        return Text(
            snapshot.data.toString(),
            style: const TextStyle(color: Colors._red_, fontSize: 40)); }else {
        return const Text('Empty data'); }}else {
      return Text('State: ${snapshot.connectionState}'); }}),Copy the code

AsyncSnapshot also has a property called hasError that can be used to check if a snapshot contains a non-null error value. The hasError value is valid if the latest result of the asynchronous activity fails. To get information, you can first check whether the snapshot contains information by getting its hasData property, which will be valid if the Stream effectively frees any non-null values. Then, at this point, you can get information from the data properties of AsyncSnapshot.

Because of the values of the above properties, you can calculate what should be rendered on the screen. In the code below, when connectionState value is waiting for, will show a CircularProgressIndicator. When the connectionState changes to Active or done, the snapshot can be checked for errors or information. The build function is called the detection of the Flutter pipe. Therefore, it will get a time-dependent snapshot subgroup. This means that if the Stream emits some values at virtually similar times, some of the values might not be passed to the builder.

Enumerations have some possible values:

  • > None: none: is not associated with any asynchronous calculation. May occur if the stream is empty
  • > waiting: associated with an asynchronous computation and waiting for collaboration. In this context, it implies that the flow is not yet complete
  • > active: active: associated with active asynchronous computation. For example, if a Stream has returned any value, but is not finished yet
  • > done: > done: associated with terminated asynchronous computation. In this context, it implies that the flow is complete

Set initial data:

You can choose to pass a worth as an initialData parameter, which will be used until the Stream emits a. If the value passed is not null, the hasData property will first be true in any event while connectionState is waiting

StreamBuilder<int>(
initialData: 0.// other arguments
)
Copy the code

To display the initial data while connectionState is waiting, adjust if snapshot.connectionState = = ConnectionState.waiting and then adjust the block in the code above.

if (snapshot.connectionState == ConnectionState.waiting) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      CircularProgressIndicator(),
      Visibility(
        visible: snapshot.hasData,
        child: Text(
          snapshot.data.toString(),
          style: const TextStyle(color: Colors._black_, fontSize: 24(), [[(), [(). }Copy the code

When we run the application, we should get screen output like the screen video below.

Code File:

Password file:

import 'package:flutter/material.dart';
import 'package:flutter_steambuilder_demo/splash_screen.dart';

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

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Splash(),
      debugShowCheckedModeBanner: false,); } } Stream<int> generateNumbers = (() async* {
  await Future<void>.delayed(Duration(seconds: 2));

  for (int i = 1; i <= 10; i++) {
    await Future<void>.delayed(Duration(seconds: 1));
    yield i;
  }
})();

class StreamBuilderDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return_StreamBuilderDemoState (); }}class _StreamBuilderDemoState extends State<StreamBuilderDemo> {

  @override
  initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text('Flutter StreamBuilder Demo'),
      ),
      body: SizedBox(
        width: double._infinity_,
        child: Center(
          child: StreamBuilder<int>(
            stream: generateNumbers,
            initialData: 0,
            builder: (
                BuildContext context,
                AsyncSnapshot<int> snapshot,
                ) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CircularProgressIndicator(),
                    Visibility(
                      visible: snapshot.hasData,
                      child: Text(
                        snapshot.data.toString(),
                        style: const TextStyle(color: Colors._black_, fontSize: 24(), [[(), [(). }else if (snapshot.connectionState == ConnectionState.active
                  || snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  return const Text('Error');
                } else if (snapshot.hasData) {
                  return Text(
                      snapshot.data.toString(),
                      style: const TextStyle(color: Colors._red_, fontSize: 40)); }else {
                  return const Text('Empty data'); }}else {
                return Text('State: ${snapshot.connectionState}'); }},),),),); }}Copy the code

Conclusion:

In this article, I’ve covered the basic structure of the StreamBuilder; You can modify this code as you choose. This is my little introduction to StreamBuilder On User Interaction, which is working with Flutter.


The elder brother of the © cat

  • ducafecat.tech/

  • github.com/ducafecat

  • Ducafecat WeChat group

  • B station space.bilibili.com/404904528

The issue of

Open source

GetX Quick Start

Github.com/ducafecat/g…

News client

Github.com/ducafecat/f…

Strapi manual translation

getstrapi.cn

Wechat discussion group Ducafecat

A series of collections

The translation

Ducafecat. Tech/categories /…

The open source project

Ducafecat. Tech/categories /…

Dart programming language basics

Space.bilibili.com/404904528/c…

Start Flutter with zero basics

Space.bilibili.com/404904528/c…

Flutter combat news client from scratch

Space.bilibili.com/404904528/c…

Flutter component development

Space.bilibili.com/404904528/c…

Flutter Bloc

Space.bilibili.com/404904528/c…

Flutter Getx4

Space.bilibili.com/404904528/c…

Docker Yapi

Space.bilibili.com/404904528/c…