实时数据库列表视图的问题



我正在尝试将realTime数据库与一个flutter应用程序连接,以便将数据下载到列表视图。当我试图在列表视图中显示数据时,我遇到了一个问题,因为它总是显示默认文本。我在android工作室的控制台上打印数据,我看到数据下载正确,但列表视图看不到。

我发布以下代码:

class _ReadUsersDetailsState extends State<ReadUsersDetails> {
List<FireBaseFunction> list = [];
DatabaseReference databaseReference =
FirebaseDatabase.instance.reference().child("DataBase");
@override
void initState() {
super.initState();
chargeData();
print("List: $list");
}
void chargeData() {
databaseReference.once().then((DataSnapshot snap) {
var keys = snap.value.keys;
var data = snap.value;
for (var key in keys) {
FireBaseFunction fireBaseFunction =
new FireBaseFunction(data[key]['Name'], data[key]['Surname']);
list.add(fireBaseFunction);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('Database'),
),
body: Container(
child: createListView(),
),
);
}
Widget createListView() {
print("List1: $list");
return list.length == 0
? new Text("Data not available")
: new ListView.builder(
itemBuilder: (_, index) {
return interface(list[index].name, list[index].surname);
},
itemCount: list.length,
);
}
Widget interface(String name, String surname) {
return Card(
child: Container(
height: 90,
child: Padding(
padding: EdgeInsets.only(top: 20, bottom: 20),
child: Column(
children: <Widget>[
new Text("Name: " + name),
new Text("Surname: " + surname),
],
),
),
),
);
}
}

更改数据后,您需要调用setState(),以告知小部件有关更改的信息,以及它需要重新发送。

void chargeData() {
databaseReference.once().then((DataSnapshot snap) {
var keys = snap.value.keys;
var data = snap.value;
for (var key in keys) {
FireBaseFunction fireBaseFunction =
new FireBaseFunction(data[key]['Name'], data[key]['Surname']);
list.add(fireBaseFunction);
}
setState(() {});
});
}

最新更新