gpt4 book ai didi

dart - flutter :未处理的异常:错误状态:调用关闭后无法添加新事件

转载 作者:行者123 更新时间:2023-12-01 16:13:13 26 4
gpt4 key购买 nike

我正在尝试使用 block 模式来管理来自 API 的数据并在我的小部件中显示它们。我能够从 API 获取数据并处理并显示它,但我使用底部导航栏,当我更改选项卡并转到上一个选项卡时,它会返回此错误:

Unhandled Exception: Bad state: Cannot add new events after calling close.

我知道这是因为我正在关闭流,然后尝试添加它,但我不知道如何修复它,因为不处理publishsubject将导致内存泄漏。这是我的用户界面代码:

class CategoryPage extends StatefulWidget {
@override
_CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
@override
void initState() {
serviceBloc.getAllServices();
super.initState();
}

@override
void dispose() {
serviceBloc.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: serviceBloc.allServices,
builder: (context, AsyncSnapshot<ServiceModel> snapshot) {
if (snapshot.hasData) {
return _homeBody(context, snapshot);
}
if (snapshot.hasError) {
return Center(
child: Text('Failed to load data'),
);
}
return CircularProgressIndicator();
},
);
}
}

_homeBody(BuildContext context, AsyncSnapshot<ServiceModel> snapshot) {
return Stack(
Padding(
padding: EdgeInsets.only(top: screenAwareSize(400, context)),
child: _buildCategories(context, snapshot))
],
);
}

_buildCategories(BuildContext context, AsyncSnapshot<ServiceModel> snapshot) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3, crossAxisSpacing: 3.0),
itemCount: snapshot.data.result.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
child: CategoryWidget(
title: snapshot.data.result[index].name,
icon: Icons.phone_iphone,
),
onTap: () {},
);
},
),
);
}

这是我的 block 代码:

class ServiceBloc extends MainBloc {
final _repo = new Repo();
final PublishSubject<ServiceModel> _serviceController =
new PublishSubject<ServiceModel>();
Observable<ServiceModel> get allServices => _serviceController.stream;
getAllServices() async {
appIsLoading();
ServiceModel movieItem = await _repo.getAllServices();
_serviceController.sink.add(movieItem);
appIsNotLoading();
}

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

ServiceBloc serviceBloc = new ServiceBloc();

我没有包含存储库和 API 代码,因为它不属于此错误的主题。

最佳答案

使用StreamController.isClosed检查 Controller 是否关闭,如果未关闭则向其添加数据。

if (!_controller.isClosed) 
_controller.sink.add(...); // safe to add data as _controller isn't closed yet
<小时/>

来自文档:

Whether the stream controller is closed for adding more events.

The controller becomes closed by calling the close method. New events cannot be added, by calling add or addError, to a closed controller.

If the controller is closed, the "done" event might not have been delivered yet, but it has been scheduled, and it is too late to add more events.

关于dart - flutter :未处理的异常:错误状态:调用关闭后无法添加新事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55536461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com