无法从 Firebase 数据库中获取数据
cannot fetch data from Firebase database
我已经这样做了,但它对我说 class bool has no instance method [] reciver null tried
调用 []ok
class Test7 extends StatefulWidget {
@override
_Test7State createState() => _Test7State();
}
class _Test7State extends State<Test7> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple,
appBar: AppBar(),
body: SingleChildScrollView(
child: Flexible(
child: FirebaseAnimatedList(
shrinkWrap: true,
query: FirebaseDatabase.instance.reference().child(currentUser.uid), // when i remove child(currentUser.uid)it is work !
itemBuilder: (BuildContext context, DataSnapshot snapshot,Animation<double> animation, int index) {
return ListTile(
leading: IconButton(
onPressed: () { },
icon: Icon(Icons.ac_unit),
),
title: Text(snapshot.value["ok"]),
);
},),
),
),
);
}
}
我在 Firebase 数据库中实时拥有这个
NUuhYzfVS9YPYDIox3X6ozhzFiT2
activity: true
ok: "ok"
A FirebaseAnimatedList
显示您在query
中指定的数据库路径下的子节点列表。如果您在该路径中指定 UID,您最终会得到这两个子节点:
activity: true
ok: "ok"
所以您的 itemBuilder
被调用了两次:一次是 activity: true
,一次是 ok: "ok"
。然后当你在 snapshot
上调用 snapshot.value["ok"]
时,就没有这样的 child 属性 了。
底线:FirebaseAnimatedList
只有在显示用户列表时才有意义。当您显示特定用户时,您不能在 FirebaseAnimatedList
中显示它,您通常希望在 Text
小部件中显示它,使用(例如)FutureBuilder
:
FutureBuilder(
future: FirebaseDatabase.instance.reference().child(currentUser.uid).get(),
builder:
(BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data.value["ok"]);
}
return Text("loading");
},
我已经这样做了,但它对我说 class bool has no instance method [] reciver null tried 调用 []ok
class Test7 extends StatefulWidget {
@override
_Test7State createState() => _Test7State();
}
class _Test7State extends State<Test7> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple,
appBar: AppBar(),
body: SingleChildScrollView(
child: Flexible(
child: FirebaseAnimatedList(
shrinkWrap: true,
query: FirebaseDatabase.instance.reference().child(currentUser.uid), // when i remove child(currentUser.uid)it is work !
itemBuilder: (BuildContext context, DataSnapshot snapshot,Animation<double> animation, int index) {
return ListTile(
leading: IconButton(
onPressed: () { },
icon: Icon(Icons.ac_unit),
),
title: Text(snapshot.value["ok"]),
);
},),
),
),
);
}
}
我在 Firebase 数据库中实时拥有这个
NUuhYzfVS9YPYDIox3X6ozhzFiT2
activity: true
ok: "ok"
A FirebaseAnimatedList
显示您在query
中指定的数据库路径下的子节点列表。如果您在该路径中指定 UID,您最终会得到这两个子节点:
activity: true
ok: "ok"
所以您的 itemBuilder
被调用了两次:一次是 activity: true
,一次是 ok: "ok"
。然后当你在 snapshot
上调用 snapshot.value["ok"]
时,就没有这样的 child 属性 了。
底线:FirebaseAnimatedList
只有在显示用户列表时才有意义。当您显示特定用户时,您不能在 FirebaseAnimatedList
中显示它,您通常希望在 Text
小部件中显示它,使用(例如)FutureBuilder
:
FutureBuilder(
future: FirebaseDatabase.instance.reference().child(currentUser.uid).get(),
builder:
(BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data.value["ok"]);
}
return Text("loading");
},