【发布时间】:2020-06-06 02:42:06
【问题描述】:
我正在尝试将我的实时数据库与颤振应用程序连接以将数据下载到列表视图。当我尝试在我的列表视图上显示数据时遇到问题,因为它始终显示默认文本。我在 android studio 的控制台上打印数据,我看到数据已正确下载,但 listview 看不到它们。
我把代码贴在下面:
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),
],
),
),
),
);
}
}
【问题讨论】:
-
您是否尝试过使用FutureBuilder?
-
是的,但它不起作用
标签: listview flutter firebase-realtime-database