我正在学习 Flutter 并想制作一个Widget就像内置的CircleAvatar. 但是,我希望这种行为是
NetworkImage) 和首字母(即 BB)以下代码可以正常工作,但在聊天演示中使用时,由于添加了多个 MyAvatar,它会崩溃。initState 上的断点显示它总是使用输入的第一个消息文本调用 - 不是我所期望的。它也会随着图像“重新加载”而闪烁。小部件似乎以我不明白的方式被重用。
class MyAvatar extends StatefulWidget {
NetworkImage image;
MyAvatar({this.text}) {
debugPrint("MyAvatar " + this.text);
if (text.contains('fun')) {
this.image = new NetworkImage("https://cdn3.iconfinder.com/data/icons/minicons-for-web-sites/24/minicons2-14-512.png");
}
}
final String text;
@override
MyAvatarState createState() {
return new MyAvatarState();
}
}
class MyAvatarState extends State<MyAvatar> {
bool showImage = false;
@override
initState() {
super.initState();
if (widget.image != null) {
var completer = widget.image.load(widget.image);
completer.addListener((info, sync) { …Run Code Online (Sandbox Code Playgroud) My usecase is this: I connect to a service with a websocket and get periodic (but unpredictable) health data from the service. The app may have multiple users of this data stream, so I want to share it. New subscribers should see the most recently emitted health data. I also want to close the websocket when there are no more subscribers.
My app used shareReplay(1) for quite some time, until it was discovered that it leaks the underlying connection ( …