我正在尝试在触发侦听器时获取路径或父级 例如。
// Setting Listener on friends info CHANGE
database.reference().child('user/$k/info').onChildChanged.listen((event){
print('Triggered Listener on -- CHANGED -- friend info');
// I want to get the variable k here or the parent value of info when the
// listener is triggered
}),
答案 0 :(得分:0)
我的同事帮助我更好地了解了闭包。因为我在forEach中调用了listen函数,所以当侦听器触发时,我可以访问$ k变量。
var values = new Map<String, dynamic>.from(snapshot.value);
values.forEach((k, v) =>
{
// k represents each friend's uid pulled from the user's friend list
// Settings Listener on friends info CHANGE
database.reference().child('user/$k/info').onChildChanged.listen((event){
print('$k'); // <- The user ID I was looking for
print('Triggered Listener on -- CHANGED -- friend info');
print('info that changed: ${event.snapshot.key}: ${event.snapshot.value}');
}),```