我正在尝试使用模型将 Firebase 实时数据库中的数据检索到 Flutter 中的列表中。当我这样做时,我的列表返回为空。我已经阅读了其他几篇关于在 Flutter 中使用 Firebase 的帖子,但没有找到明确的答案。这是我目前的方法(该对象称为“需求”,我正在尝试检索并显示需求列表):
模型:
import 'package:firebase_database/firebase_database.dart';
class Need {
final String id;
final String imageUrl;
final String caption;
final String title;
Need({
this.id,
this.imageUrl,
this.caption,
this.title,
});
Need.fromSnapshot(DataSnapshot snapshot) :
id = snapshot.key,
imageUrl = snapshot.value["imageUrl"],
caption = snapshot.value["caption"],
title = snapshot.value["postTitle"];
toJson() {
return {
"imageUrl": imageUrl,
"caption": caption,
"title": title,
};
}
}
Run Code Online (Sandbox Code Playgroud)
使用 Firebase 查询的数据库服务:
import 'package:firebase_database/firebase_database.dart';
import 'package:Given_Flutter/models/need_model.dart';
class DatabaseService {
static Future<List<Need>> getNeeds() async {
Query needsSnapshot = await …
Run Code Online (Sandbox Code Playgroud)