final databaseReference = Firestore.instance.collection("classrooms");
bool hany = false;
var names = new Map<String, dynamic>();
List nevek = [];
List IDS = [];
Future<void> getclasses() async {
QuerySnapshot snapshot = await databaseReference.getDocuments();
snapshot.documents.forEach((f) {
IDS.add(f.documentID);
x = (f.data.length);
names.addAll(f.data);
for (int i = 0; i < names.values.toList().length / x; i++) {
nevek.add(names.values.toList()[i + 1]);
}
});
}
class ScrollableClassroom extends StatefulWidget {
@override
_ScrollableClassroomState createState() => _ScrollableClassroomState();
}
class _ScrollableClassroomState extends State<ScrollableClassroom> {
@override
void initState() {
super.initState();
IDS = [];
names.clear();
getclasses();
}
Widget build(BuildContext context) {
print(nevek);
print(IDS);
return WillPopScope(
onWillPop: () async => false,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Osztályok"),
leading: Padding(
padding: const EdgeInsets.only(left: 5.0),
child: IconButton(
icon: Icon(Icons.exit_to_app, color: Colors.black38),
onPressed: () {
authService.signOut();
authService.loggedIn = false;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => GoogleSignUp()));
})),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 5.0),
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.add_circle_outline,
color: Colors.black38),
onPressed: null),
IconButton(
icon: Icon(Icons.search, color: Colors.black38),
onPressed: null),
],
)),
],
),
body: ListView.builder(
itemCount: nevek.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {
id = IDS[index];
Navigator.push(context,
MaterialPageRoute(builder: (context) => InClassRoom()));
},
title: Text(nevek[index]),
),
);
},
),
),
);
}
}
在Listview中,它第一次不显示任何内容,但是在热重新加载后,它可以正常工作。但是,如果我进行第二次热重装,它将显示所有内容,但是显示两次。另一种情况是,当我按下应用程序栏左侧的后退按钮时,它将带您进入登录屏幕。如果您是第一次从登录屏幕返回,那么很好,但是如果再次执行此操作,它将再次显示所有内容。然后那三遍,等等。
答案 0 :(得分:1)
首先,在映射完文档后,您缺少setState
将状态变量保留在状态类中
答案 1 :(得分:1)
尝试以下代码:
class ScrollableClassroom extends StatefulWidget {
@override
_ScrollableClassroomState createState() => _ScrollableClassroomState();
}
class _ScrollableClassroomState extends State<ScrollableClassroom> {
//moved from the Widget class
final databaseReference = Firestore.instance.collection("classrooms");
bool hany = false;
var names = new Map<String, dynamic>();
List nevek = [];
List IDS = [];
Future<void> getclasses() async {
QuerySnapshot snapshot = await databaseReference.getDocuments();
snapshot.documents.forEach((f) {
IDS.add(f.documentID);
x = (f.data.length);
names.addAll(f.data);
for (int i = 0; i < names.values.toList().length / x; i++) {
nevek.add(names.values.toList()[i + 1]);
}
});
setState((){}); // <- this line tells the StetefulWidget that it's state has been changed and it needs to rebuild
}
...(the rest of the state class)