我正在从消防站获取音乐模型数据。我正在使用StreamBuilder来显示它们。我有一个列表(mList),可以在其中添加音乐。添加,删除所有内容都可以。问题是当我在Firestore中更改音乐值时,StreamBuilder立即更改了该值,但是list(mList)仍然是旧值,没有任何更改。该如何解决?
List<Music> mList = List<Music>();
Music music = Music();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
child: StreamBuilder(
stream: fr.getList(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Text('0');
} else if (snapshot.hasError) {
return Text('error');
}
List<Music> list = snapshot.data;
return Expanded(
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, i) {
music = list[i];
return Column(
children: [
SizedBox(height: 50),
Container(
child: InkWell(
child: Text(music.name),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MusDetails(
music: music,
mList: mList,
)));
},
),
),
],
);
}),
);
}),
),
SizedBox(height: 50),
Expanded(
child: ListView.builder(
itemCount: mList.length,
itemBuilder: (BuildContext context, int i) {
return Center(
child: Text(mList[i].name),
);
},
),
)
],
),
);
}
MusDetails页面中的添加音乐,用于添加的简单代码为:mList.add(music)。