Flutter:如何获取流数据?

问题描述 投票:-1回答:1

我仅使用StreamControllerInheritedWidget,而不使用第三方程序包。

在我的应用中,我有一个BLoCHomeScreenListScreen

  1. 我的BLoC是HomeScreen的父级,用于流式播放数字列表
  2. HomePage中,我只有一个按钮可以打开ListScreenHomePage不使用BLoC
  3. ListScreen中,我使用StreamBuilder获取数字列表,但我的连接状态始终在等待。

BLoC打开时,我怎么打电话给ListScreen发送清单。

如果我在BLoC的构造函数中发送列表,那么在打开ListScreen连接状态后仍在等待。

我的来源:https://github.com/nguu/learning-flutter-stream

flutter dart
1个回答
0
投票

您可以使用initialData初始化数据工作演示和修改的两个文件的内容,您可以在下面看到>

代码段

 body: StreamBuilder(
        stream: this.bloc.data,
        initialData: this.bloc.addInit(),
...
Future<void> addInit() async {
    for (var i = 0; i < 10; i++) {
      await Future.delayed(const Duration(milliseconds: 10));
      this._items.add(i);
    }
    this.streaming();
  }

工作演示

enter image description here

list_bloc.dart

import 'dart:async';

import 'bloc.dart';

class ListBloc implements Bloc {
  ListBloc() {
    //this.addInit();
  }

  final _listSubject = StreamController<List<int>>.broadcast();
  Stream<List<int>> get data => _listSubject.stream;
  List<int> _items = <int>[];

  Future<void> addInit() async {
    for (var i = 0; i < 10; i++) {
      await Future.delayed(const Duration(milliseconds: 10));
      this._items.add(i);
    }
    this.streaming();
  }

  bool add (int number) {
    this._items.add(number);
    this.streaming();
    return true;
  }

  void streaming() {
    _listSubject.sink.add(this._items);
  }

  void dispose() {
    _listSubject.close();
  }
}

list_screen.dart

import 'dart:math';

import 'package:flutter/material.dart';
import 'list_bloc.dart';
import 'provider.dart';

class ListScreen extends StatefulWidget {
  ListScreen({
    Key key,
  }): super(key: key);

  /// Creates the mutable state for this widget at a given location in the tree.
  @override
  State<ListScreen> createState() => _ListScreenState();
}

class _ListScreenState extends State<ListScreen> {
  ListBloc bloc;
  /// Called when this state first inserted into tree.
  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    this.bloc = Provider.of(context).component;
  }

  /// Called whenever the widget configuration changes.
  @override
  void didUpdateWidget(ListScreen old) {
    super.didUpdateWidget(old);
  }

  /// Called when this state removed from the tree.
  @override
  void dispose() {
    super.dispose();
  }

  /// Build this state.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: this.bloc.data,
        initialData: this.bloc.addInit(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          } else {
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (BuildContext context, int index) {
                  final int item = snapshot.data[index];
                  return ListTile(
                    title: Text(item.toString()),
                  );
                },
              );
            } else {
              return Text('empty');
            }
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          var ran = Random();
          this.bloc.add(ran.nextInt(50));
        },
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.