【发布时间】:2018-11-04 07:58:20
【问题描述】:
我想使用 Stream 对象的 firebase 子添加方法从我的 firebase 实时数据库中检索列表。我已经像下面这样配置了我的应用程序,但我只加载了 1 个“问题”(最新的),其余的根本没有加载。
我的数据库中确实有超过 5 个问题。如何从我的实时数据库中获取 5 个最新问题的列表?
class _QuestionPageState extends State<QuestionsPage> {
List _questions = [];
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
body: new StreamBuilder(
stream: FirebaseDatabase.instance
.reference()
.child('questions')
.limitToLast(5)
.onChildAdded,
builder: (context, snap) {
if (snap.hasError) return new Text('Error: ${snap.error}');
if (snap.data == null)
return new Center(
child: new CircularProgressIndicator(),
);
print(snap.data.snapshot.key);
print(snap.data.snapshot);
final question = snap.data.snapshot.value;
this._questions.add(question);
return new ListView.builder(
itemCount: this._questions.length,
itemBuilder: (context, index) {
print("question");
print(this._questions[index]["meta"]["question"]);
return new Text(this._questions[index]["meta"]["question"]);
},
);
},
),
);
}
}
[更新] 这是数据库结构
【问题讨论】:
-
能否将您的数据库文件附加到这个问题中?
-
@AkshayNandwana 我刚刚添加了我的数据库的图像
-
@farhana 当然,去吧
-
@farhana 我觉得自己很愚蠢,大声笑继续!
标签: firebase firebase-realtime-database dart flutter