带有两个模拟器的 StreamBuilder 数据
StreamBuilder data with two Emulators
当我打开两个模拟器时,我遇到了奇怪的(据我所知)StreamListener 数据行为。
我有一段代码可以在 ListView 中读取 Firebase 的 RealtimeDB 和 return。
body: StreamBuilder(
stream: _database.onValue,
builder: (context, snapshot) {
List<FirstRoundQuestion> frqList = [];
if (snapshot.hasData) {
final firstRoundQuestion = Map<dynamic, dynamic>.from(
(snapshot.data! as DatabaseEvent).snapshot.value
as Map<dynamic, dynamic>);
firstRoundQuestion.forEach((key, value) {
final frq = Map<String, dynamic>.from(value);
frqList.add(FirstRoundQuestion(
question: frq['question'],
answer: frq['answer'],
answered: frq['answered']));
});
return ListView.builder(
reverse: true,
physics: NeverScrollableScrollPhysics(),
controller: _controller.pageController,
itemCount: frqList.length,
itemBuilder: (context, index) {
return Row(
children: [
Text('${frqList[index].question}'),
TextButton(
onPressed: () {}
child: Text('${frqList[index].answered}'))
],
);
},
);
} else {
return Text("Doesn't work");
}
}));
在移动模拟器上构建应用程序然后在边缘模拟器上构建应用程序时,我得到的是此列表的两个不同顺序,第一个是升序,另一个是降序。伙计们,我在这里想念什么? :o
希望我解释清楚了。
要按特定顺序显示项目,您需要做两件事:
- 从数据库中按该顺序检索项目。
- 然后按照您在代码中收到它们的顺序处理它们。
如果您还没有从数据库中请求特定顺序的项目,例如您可以这样做:
body: StreamBuilder(
stream: _database.orderByKey().onValue,
...
一旦您按特定顺序请求项目,您构建器中的这段代码无法按顺序处理项目:
if (snapshot.hasData) {
final firstRoundQuestion = Map<dynamic, dynamic>.from(
(snapshot.data! as DatabaseEvent).snapshot.value
as Map<dynamic, dynamic>);
firstRoundQuestion.forEach((key, value) {
final frq = Map<String, dynamic>.from(value);
frqList.add(FirstRoundQuestion(
question: frq['question'],
answer: frq['answer'],
answered: frq['answered']));
});
地图中项目的顺序未定义,因此当您将快照的整个值转换为地图时,您将失去项目的顺序。
按顺序处理项目的解决方案是循环快照的children
属性:
(snapshot.data! as DatabaseEvent).snapshot.children.forEach((snapshot) {
final frq = Map<Sting, dynamic>.from(snapshot.value as Map<dynamic, dynamic>);
frqList.add(FirstRoundQuestion(
question: frq['question'],
answer: frq['answer'],
answered: frq['answered']));
})
当我打开两个模拟器时,我遇到了奇怪的(据我所知)StreamListener 数据行为。
我有一段代码可以在 ListView 中读取 Firebase 的 RealtimeDB 和 return。
body: StreamBuilder(
stream: _database.onValue,
builder: (context, snapshot) {
List<FirstRoundQuestion> frqList = [];
if (snapshot.hasData) {
final firstRoundQuestion = Map<dynamic, dynamic>.from(
(snapshot.data! as DatabaseEvent).snapshot.value
as Map<dynamic, dynamic>);
firstRoundQuestion.forEach((key, value) {
final frq = Map<String, dynamic>.from(value);
frqList.add(FirstRoundQuestion(
question: frq['question'],
answer: frq['answer'],
answered: frq['answered']));
});
return ListView.builder(
reverse: true,
physics: NeverScrollableScrollPhysics(),
controller: _controller.pageController,
itemCount: frqList.length,
itemBuilder: (context, index) {
return Row(
children: [
Text('${frqList[index].question}'),
TextButton(
onPressed: () {}
child: Text('${frqList[index].answered}'))
],
);
},
);
} else {
return Text("Doesn't work");
}
}));
在移动模拟器上构建应用程序然后在边缘模拟器上构建应用程序时,我得到的是此列表的两个不同顺序,第一个是升序,另一个是降序。伙计们,我在这里想念什么? :o
希望我解释清楚了。
要按特定顺序显示项目,您需要做两件事:
- 从数据库中按该顺序检索项目。
- 然后按照您在代码中收到它们的顺序处理它们。
如果您还没有从数据库中请求特定顺序的项目,例如您可以这样做:
body: StreamBuilder(
stream: _database.orderByKey().onValue,
...
一旦您按特定顺序请求项目,您构建器中的这段代码无法按顺序处理项目:
if (snapshot.hasData) {
final firstRoundQuestion = Map<dynamic, dynamic>.from(
(snapshot.data! as DatabaseEvent).snapshot.value
as Map<dynamic, dynamic>);
firstRoundQuestion.forEach((key, value) {
final frq = Map<String, dynamic>.from(value);
frqList.add(FirstRoundQuestion(
question: frq['question'],
answer: frq['answer'],
answered: frq['answered']));
});
地图中项目的顺序未定义,因此当您将快照的整个值转换为地图时,您将失去项目的顺序。
按顺序处理项目的解决方案是循环快照的children
属性:
(snapshot.data! as DatabaseEvent).snapshot.children.forEach((snapshot) {
final frq = Map<Sting, dynamic>.from(snapshot.value as Map<dynamic, dynamic>);
frqList.add(FirstRoundQuestion(
question: frq['question'],
answer: frq['answer'],
answered: frq['answered']));
})