- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 RxDart 来观察变化并相应地更新 UI。当应用程序启动时,我正在进行网络调用并成功获取数据,观察更改并相应地更新 UI。但是当我在关闭屏幕时处理 Subjects
时。它给出以下错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (15524): The following StateError was thrown while finalizing the widget tree:
I/flutter (15524): Bad state: You cannot close the subject while items are being added from addStream
这里是 bloc 类:
class MovieDetailBloc {
final _repository = Repository();
final _movieId = PublishSubject<int>();
final _trailers = BehaviorSubject<Future<TrailerModel>>();
Function(int) get fetchTrailersById => _movieId.sink.add;
Observable<Future<TrailerModel>> get movieTrailers => _trailers.stream;
MovieDetailBloc() {
_movieId.stream.transform(_itemTransformer()).pipe(_trailers);
}
dispose() {
_movieId.close();
_trailers.close();
}
_itemTransformer() {
return ScanStreamTransformer(
(Future<TrailerModel> trailer, int id, int index) {
print(index);
trailer = _repository.fetchTrailers(id);
return trailer;
},
);
}
}
这是我称之为的 UI 屏幕:
import 'dart:async';
import 'package:flutter/material.dart';
import '../blocs/movie_detail_bloc_provider.dart';
import '../models/trailer_model.dart';
class MovieDetail extends StatefulWidget {
final posterUrl;
final description;
final releaseDate;
final String title;
final String voteAverage;
final int movieId;
MovieDetail({
this.title,
this.posterUrl,
this.description,
this.releaseDate,
this.voteAverage,
this.movieId,
});
@override
State<StatefulWidget> createState() {
return MovieDetailState(
title: title,
posterUrl: posterUrl,
description: description,
releaseDate: releaseDate,
voteAverage: voteAverage,
movieId: movieId,
);
}
}
class MovieDetailState extends State<MovieDetail> {
final posterUrl;
final description;
final releaseDate;
final String title;
final String voteAverage;
final int movieId;
MovieDetailBloc bloc;
MovieDetailState({
this.title,
this.posterUrl,
this.description,
this.releaseDate,
this.voteAverage,
this.movieId,
});
@override
void didChangeDependencies() {
bloc = MovieDetailBlocProvider.of(context);
bloc.fetchTrailersById(movieId);
super.didChangeDependencies();
}
@override
void dispose() {
bloc.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 220.0,
floating: false,
pinned: false,
flexibleSpace: FlexibleSpaceBar(
background: Image.network(
"https://image.tmdb.org/t/p/w500$posterUrl",
fit: BoxFit.cover,
)),
),
];
},
body: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(margin: EdgeInsets.only(top: 5.0)),
Text(
title,
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
Container(margin: EdgeInsets.only(top: 8.0, bottom: 8.0)),
Row(
children: <Widget>[
Icon(
Icons.favorite,
color: Colors.red,
),
Container(
margin: EdgeInsets.only(left: 1.0, right: 1.0),
),
Text(
voteAverage,
style: TextStyle(
fontSize: 18.0,
),
),
Container(
margin: EdgeInsets.only(left: 10.0, right: 10.0),
),
Text(
releaseDate,
style: TextStyle(
fontSize: 18.0,
),
),
],
),
Container(margin: EdgeInsets.only(top: 8.0, bottom: 8.0)),
Text(description),
Container(margin: EdgeInsets.only(top: 8.0, bottom: 8.0)),
Text(
"Trailer",
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
Container(margin: EdgeInsets.only(top: 8.0, bottom: 8.0)),
StreamBuilder(
stream: bloc.movieTrailers,
builder:
(context, AsyncSnapshot<Future<TrailerModel>> snapshot) {
if (snapshot.hasData) {
return FutureBuilder(
future: snapshot.data,
builder:
(context, AsyncSnapshot<TrailerModel> itemSnapShot) {
if (itemSnapShot.hasData) {
if (itemSnapShot.data.results.length > 0)
return trailerLayout(itemSnapShot.data);
else
return noTrailer(itemSnapShot.data);
} else {
return CircularProgressIndicator();
}
},
);
} else {
return CircularProgressIndicator();
}
},
),
],
),
),
),
);
}
Widget noTrailer(TrailerModel data) {
return Center(
child: Container(
child: Text("No trailer available"),
),
);
}
}
Widget trailerLayout(TrailerModel data) {
return Row(
children: <Widget>[
Expanded(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.all(5.0),
height: 100.0,
color: Colors.grey,
),
Text(
data.results[0].name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
Expanded(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.all(5.0),
height: 100.0,
color: Colors.grey,
),
Text(
data.results[1].name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
],
);
}
当我关闭屏幕时出现错误。 dispose 方法抛出上述异常。我该如何解决这个问题?
最佳答案
我认为排干流应该可以解决问题
dispose() async {
_movieId.close();
await _trailers.drain();
_trailers.close();
}
https://api.dartlang.org/stable/2.0.0/dart-async/Stream-class.html
关于dart - 错误状态 : You cannot close the subject while items are being added from addStream in flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191451/
此错误显然源于 xlsxwriter。我不确定它来自我的代码的哪一行,因为每次我尝试调试时,我的编辑器 Visual Studio 2019 都会崩溃。但是,在使用 VPN 和远程桌面连接时,我在笔记
我有一个用于原型(prototype)的游戏数据表。我在工作时生成数据,但当我离开并且我的机器进入休眠状态时,数据生成停止。这导致我的元素收藏出现很大差距。 我希望能够移动表格的 DateTimeCr
我正在使用wavesurfer在我的网页上显示歌曲波形。我正在使用以下代码 - function setupSongwaves(songJson) { var songwaveid = '#s
我是 JDBC 新手... Student类有Constructor、add()、update()和delete()等方法... 在构造函数中打开连接。下面代码中的 conn.close() 和 ps
考虑以下代码,它是许多 ChannelFactory 示例的典型代码: WSHttpBinding myBinding = new WSHttpBinding(); EndpointAddress m
我正在阅读 Java Data Access — JDBC、JNDI 和 JAXP,了解 Connection、PooledConnection 接口(interface)。据我了解, PooledC
我正在做我的第一个 android 学习教程,但遇到了标题描述的这个错误..这是我试图在 Eclipse 上做的应用程序,java,这是我的代码..(代码是自动生成的由项目) package com.
我正在使用 JPA、Hibernate、Jboss 和容器管理事务。当我尝试用数据保存我的大实体时,它会抛出以下异常。将我的实体视为图形模型。这个异常并不是每次都会抛出。 ERROR [org.jbo
我有 GWT 应用程序,它与 AdaptivePayment API 上的灯箱集成。 我无法使用提供的代码关闭取消/返回页面: dgFlow = top.dgFlow || top.opener.to
即使我已经实现了上述方法 close(),Eclipse 仍向我显示上述错误。 代码如下: public void update_project(View view) { EditText c
在我的网络应用程序中,我广泛使用了数据库。 我有一个抽象的 servlet,所有需要数据库连接的 servlet 都继承自它。该抽象 servlet 创建一个数据库连接,调用必须由继承 servlet
我在这里看到很多答案都说要使用 close() 来销毁套接字,但我使用的指南来自 msdn让我使用 closesocket()。我想知道是否存在差异,是否有理由使用其中一种。 在这两种情况下,我都看到
我在 python 中使用 with 语句( PEP 343 ) 时遇到了一些问题,以便在上下文之后自动管理资源清理。特别是,with 语句 始终假定资源清理方法是 .close()。 IE。在下面的
在本地连接上调用 RTCPeerConnection.close() 时,我希望远程连接接收到 closed connectionstatechange 事件。 相反,几秒钟后出现disconnect
我正在使用 netty 3.6.6。 有人可以解释以下两个代码之间的区别吗? channel.close(); channel.write(ChannelBuffers.EMPTY_BUFFER).a
WebSocket.readyState可以是CONNECTING、OPEN、CLOSING或CLOSED。 CLOSING 和 CLOSED 状态有什么区别?为什么区分这两种状态很有用?我可以将 C
想象一下,您在 Python 中打开了某个文件(无论是用于读取、写入还是其他)。我刚刚注意到,当您想关闭该文件时,您可以输入: somefile.close() 或者您可以输入: somefile.c
我在我的应用程序的各种类和线程中打开、访问、写入等数据库。我有一个数据库 self.run_params["db"] 我在整个应用程序中都使用它来访问。 问题 1:我是否应该在每次访问后关闭光标? 问
我正在尝试创建一个 vanilla JavaScript 模态,当从 HTML 文件(或 JS 文件)实例化它时,它具有由用户自定义的能力。但是,在处理关闭模式的 close() 函数时,不是一次关闭
所以这可能是一个菜鸟类型的问题,但这就是我想知道的。 假设我有两个屏幕,第一个屏幕是 idk,例如 Screen1。假设用户在 Screen1 上点击了OK,这会将他们带到Screen2。 我目前正在
我是一名优秀的程序员,十分优秀!