gpt4 book ai didi

dart - 重新打开使用 StreamBuilder 的 ExpansionTile 时流的错误状态

转载 作者:IT王子 更新时间:2023-10-29 07:04:29 24 4
gpt4 key购买 nike

第一次打开ExpansionTitle,一切正常。在收缩和扩展它之后,我收到一个已经收听过的“Bad State”流,错误。我试过将流作为广播返回,但在最小化并重新打开后,快照是空的。我不太确定将 ExpansionTitle 与流一起使用的正确技术是什么。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: StreamBuilder(
stream: _genres(),
builder:
(BuildContext context, AsyncSnapshot<List<String>> snapshot) {
print("snap: ${snapshot.data}");
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {


return ExpansionTile(
title: Text(snapshot.data[index]),
children: <Widget>[
StreamBuilder(
stream: _moviemap(index + 1),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData)
return Text('No data');


List<Widget> items = [];
for(String name in snapshot.data) {
items.add(
ListTile(
title: Text(name),
)
);
}

return Column(children: items,);
},
),
],
);
});
} else {
return Container();
}
}),
),
);
}
Stream<List<String>> _genres() async* {
yield ['Horror', 'Comedy', 'Drama'];
}

Stream<List<String>> _moviemap(int which) async* {
if (which == 1) {
yield ['Saw', 'Scary Movie'];
} else if (which == 2) {
yield ['Grown ups', 'Grown ups 2', 'Paul Blart'];
} else {
yield ['Green Onions', 'Spring Breakers'];
}
}
}

最佳答案

这是因为以下原因。

  1. StreamBuilderStreamBuilderinitState() 永久监听相同的流(_moviemap 在这种情况下)到dispose().
  2. ExpansionTile 打开和关闭时,它会重建children 的新StreamBuilderStreamBuilder 尝试监听 _moviemap。旧的 StreamBuilder 那时还没有完成 dispose()。
  3. 同一流(_moviemap) 不允许多次收听。
  4. 崩溃了。

因此您应该交换StreamBuilder 的位置和ExpansionTile 的位置来监听stream(_moviemap) 一次。祝你好运!!

示例代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: StreamBuilder(
stream: _genres(),
builder:
(BuildContext context, AsyncSnapshot<List<String>> snapshot) {
print("snap: ${snapshot.data}");
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
final title = snapshot.data[index];
return StreamBuilder(
stream: _moviemap(index + 1),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) return Text('No data');
List<Widget> items = [];
for (String name in snapshot.data) {
items.add(ListTile(
title: Text(name),
));
}
return ExpansionTile(
title: Text(title),
children: <Widget>[
Column(
children: items,
),
],
);
},
);
},
);
} else {
return Container();
}
}),
),
);
}

Stream<List<String>> _genres() async* {
yield ['Horror', 'Comedy', 'Drama'];
}

Stream<List<String>> _moviemap(int which) async* {
if (which == 1) {
yield ['Saw', 'Scary Movie'];
} else if (which == 2) {
yield ['Grown ups', 'Grown ups 2', 'Paul Blart'];
} else {
yield ['Green Onions', 'Spring Breakers'];
}
}
}

关于dart - 重新打开使用 StreamBuilder 的 ExpansionTile 时流的错误状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54660409/

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