[英]Flutter - Firebase RTDB, The method forEach was called on null
The only this I changed in my code and Firebase rtdb is where the data is being fetched from.我在代码和 Firebase rtdb 中唯一更改的是从中获取数据的位置。
Before data was in: "users" - "parents" (Code worked perfectly here)在数据进入之前:“用户”-“父母”(代码在这里完美运行)
Now data is in: "users" - schoolName.toString() - "parents" (Code causes an error)现在数据在:“users” - schoolName.toString() - “parents”(代码导致错误)
How can I approach/solve this issue?我该如何处理/解决这个问题?
Thanks.谢谢。
Error:错误:
E/flutter ( 8683): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: The method 'forEach' was called on null.
E/flutter ( 8683): Tried calling: forEach(Closure: (dynamic, dynamic) => void)
Code:代码:
Future<List> getParentDetails() async {
schoolName = await getSchoolName();
databaseReference
.child("users")
.child(schoolName.toString())
.child("parents")
.onValue
.listen(
(event) {
if (event.snapshot.exists) {
setState(
() {
var value = event.snapshot.value;
parentList = Map.from(value)
.values
.map((e) => Parents.fromJson(Map.from(e)))
.toList();
},
);
} else {
print("No Data Exists");
}
},
);
return parentList;
}
UI Code:界面代码:
ListView.builder(
itemCount: parentList.length,
itemBuilder: (context, int index) {
final Parents parents = parentList[index];
final String driverEmail = parents.email;
final String driverName = parents.name;
final String driverPhone = parents.phone;
// final driverRandomId = parents.randomId;
// final String driverUID = driver.uid;
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
elevation: 0.2,
child: ExpansionTile(
// collapsedBackgroundColor: Colors.grey,
title: Text(
driverName.toUpperCase(),
style: GoogleFonts.lexendMega(
fontSize: 12,
),
textAlign: TextAlign.center,
),
children: [
Column(
children: [
Text(
driverEmail,
textAlign: TextAlign.center,
style: GoogleFonts.lexendMega(fontSize: 12),
),
SizedBox(
height: 5,
),
Text(
driverPhone,
textAlign: TextAlign.center,
style: GoogleFonts.lexendMega(fontSize: 12),
),
SizedBox(
height: 5,
),
],
)
],
),
),
);
},
),
Class Code: Class 代码:
class Parents {
final String email;
final String name;
final String phone;
Parents({
this.email,
this.name,
this.phone,
});
static Parents fromJson(Map<String, String> json) {
return Parents(
email: json['email'],
name: json['name'],
phone: json['phone'],
);
}
}
You should be able to check whether your snapshot has some data (I'm assuming it returns an AsyncSnapshot
, which is also used by widgets like StreamBuilder
and FutureBuilder
.您应该能够检查您的快照是否有一些数据(我假设它返回一个
AsyncSnapshot
,它也被像StreamBuilder
和FutureBuilder
这样的小部件使用。
https://api.flutter.dev/flutter/widgets/AsyncSnapshot-class.html https://api.flutter.dev/flutter/widgets/AsyncSnapshot-class.html
In that case, you can call event.snapshot.hasData
to determine whether data is null. If it is, you can instead return an empty list.在这种情况下,您可以调用
event.snapshot.hasData
来确定数据是否为 null。如果是,您可以返回一个空列表。
I assume you're using this approach as opposed to a FutureBuilder
to keep your business logic and UI separate?我假设您正在使用这种方法而不是
FutureBuilder
来保持您的业务逻辑和 UI 分离? If there's no specific reasoning, you might want to consider to instead use a FutureBuilder
or StreamBuilder
instead.如果没有具体原因,您可能需要考虑改用
FutureBuilder
或StreamBuilder
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.