我只使用 StreamController 和 InheritedWidget ,而不使用第三方程序包。
在我的应用中,我有一个BLoC
,HomeScreen
和ListScreen
。
BLoC
是HomeScreen的父级,用于流式播放数字列表HomePage
中,我只有一个按钮可以打开ListScreen
,而HomePage
不使用BLoC
。ListScreen
中,我使用StreamBuilder
获取数字列表,但是我的连接状态始终在等待。 BLoC
打开时如何打电话给ListScreen
发送清单。
如果我在BLoC的构造函数中发送列表,则在打开ListScreen
连接状态后仍在等待。
答案 0 :(得分: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();
}
工作演示
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));
},
),
);
}
}