Flutter setState导致无限循环
Flutter setState causes infinite loop
我是 Flutter 的新手,我想从 Firebase 获取一次数据,但是当我在 setState 中使用 foreach 时,它会出现无限循环。我试过 initState 但我无法用 Future 弄明白。
这是我的代码:
class _AnasayfaScreenState extends State<AnasayfaScreen> {
final databaseRef = FirebaseDatabase.instance.reference();
ActivityModel activity = ActivityModel();
List<Map<dynamic, dynamic>> lists = [];
}
Future getActivity() async {
return await databaseRef
.child('activities')
.limitToLast(3)
.once()
.then((DataSnapshot dataSnapshot) {
setState(() {
Map<dynamic, dynamic> values = dataSnapshot.value;
lists.clear();
values.forEach((key, values) {
lists.add(values);
});
});
});
}
@override
Widget build(BuildContext context) {
getActivity();
return Scaffold(
appBar: AppBar(
// some stuff
title: Text("Title: " +lists[index]["title"] +"\nDate: " + lists[index]["date"] +"\nN: Number of people"+lists[index]["maxPeople"] + "\nActivity Owner: " +lists[index]["ownername"]),
任何帮助对我来说都是很棒的,谢谢。
这是您可以执行的操作的一个非常基本的示例:
class _AnasayfaScreenState extends State<AnasayfaScreen> {
final databaseRef = FirebaseDatabase.instance.reference();
ActivityModel activity = ActivityModel();
bool isLoading = true;
List<Map<dynamic, dynamic>> lists = [];
@override
void initState(){
super.initState();
getActivity();
}
Future getActivity() async {
await databaseRef
.child('activities')
.limitToLast(3)
.once()
.then((DataSnapshot dataSnapshot) {
Map<dynamic, dynamic> values = dataSnapshot.value;
lists.clear();
values.forEach((key, values) {
lists.add(values);
});
});
setState(() {
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: isLoading ? Container() // Add your widget and work here instead of Container
: CircularProgressIndicator(),
);
}
我是 Flutter 的新手,我想从 Firebase 获取一次数据,但是当我在 setState 中使用 foreach 时,它会出现无限循环。我试过 initState 但我无法用 Future 弄明白。
这是我的代码:
class _AnasayfaScreenState extends State<AnasayfaScreen> {
final databaseRef = FirebaseDatabase.instance.reference();
ActivityModel activity = ActivityModel();
List<Map<dynamic, dynamic>> lists = [];
}
Future getActivity() async {
return await databaseRef
.child('activities')
.limitToLast(3)
.once()
.then((DataSnapshot dataSnapshot) {
setState(() {
Map<dynamic, dynamic> values = dataSnapshot.value;
lists.clear();
values.forEach((key, values) {
lists.add(values);
});
});
});
}
@override
Widget build(BuildContext context) {
getActivity();
return Scaffold(
appBar: AppBar(
// some stuff
title: Text("Title: " +lists[index]["title"] +"\nDate: " + lists[index]["date"] +"\nN: Number of people"+lists[index]["maxPeople"] + "\nActivity Owner: " +lists[index]["ownername"]),
任何帮助对我来说都是很棒的,谢谢。
这是您可以执行的操作的一个非常基本的示例:
class _AnasayfaScreenState extends State<AnasayfaScreen> {
final databaseRef = FirebaseDatabase.instance.reference();
ActivityModel activity = ActivityModel();
bool isLoading = true;
List<Map<dynamic, dynamic>> lists = [];
@override
void initState(){
super.initState();
getActivity();
}
Future getActivity() async {
await databaseRef
.child('activities')
.limitToLast(3)
.once()
.then((DataSnapshot dataSnapshot) {
Map<dynamic, dynamic> values = dataSnapshot.value;
lists.clear();
values.forEach((key, values) {
lists.add(values);
});
});
setState(() {
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: isLoading ? Container() // Add your widget and work here instead of Container
: CircularProgressIndicator(),
);
}