我目前面临Flutter和Firebase的问题 .
我想显示Firebase Databse中的数据 .
大部分代码来自Fluttery关于Tinder Cards的视频,我只是想学习如何在卡片上显示姓名,照片,年龄和传记 . 我现在只想做名字和描述 .
我目前的代码:
class _ProfileCardState extends State<ProfileCard> {
Widget _buildProfileSynopsis() {
return new Positioned(
left: 0.0,
right: 0.0,
bottom: 0.0,
child: new Container(
padding: const EdgeInsets.all(24.0),
child: new Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new StreamBuilder<Event>(
stream: FirebaseDatabase.instance
.reference()
.child('items')
.child('-LD2vzOCd54yEFNXJpGj')
.onValue,
builder: (BuildContext context, AsyncSnapshot<Event> event) {
if (!event.hasData)
return new Center(child: new Text('Loading...'));
Map<dynamic, dynamic> data = event.data.snapshot.value;
return new Text('${event.data.snapshot.value}', style: new TextStyle(
fontSize: 30.0)
);
},
),
new StreamBuilder<Event>(
stream: FirebaseDatabase.instance
.reference()
.child('items')
.child('-LD2vzOCd54yEFNXJpGj')
.onValue,
builder: (BuildContext context, AsyncSnapshot<Event> event) {
if (!event.hasData)
return new Center(child: new Text('Loading...'));
Map<dynamic, dynamic> data = event.data.snapshot.value;
return new Text('-${event.data.snapshot.value}');
},
),
],
),
),
new Icon(
Icons.info,
color: Colors.black,
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return new Container(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(10.0),
boxShadow: [
new BoxShadow(
color: const Color(0x11000000),
blurRadius: 5.0,
spreadRadius: 2.0,
),
],
),
child: ClipRRect(
borderRadius: new BorderRadius.circular(10.0),
child: new Material(
child: new Stack(
fit: StackFit.expand,
children: <Widget>[
_buildProfileSynopsis(),
],
),
),
),
);
}
class Males {
String name, description;
Males(this.name, this.description);
}
}
图片:
正如您在Firebase的屏幕截图中看到的那样,每个人都有一个唯一的密钥,这使得很难引用单个数据 . 这是我的主要问题,因为我不知道如何在一个字符串中保存accountkey,所以我可以做.child(accountkey) .
所有youtube tuturials都显示了Listview,但这只是在卡片上的列表中显示数据,这是无用的,因为每张卡片都显示每个用户 .
在此先感谢您,如果您有任何疑问,请与我们联系 . 我希望你能帮忙:)
2 回答
你非常接近 .
您声明Map
Map<dynamic, dynamic> data = event.data.snapshot.value;
但不使用它,而只是在Text
小部件中使用event.data.snapshot.value
.event.data.snapshot.value
是Firebase中子项下的所有数据 . 这就是您在名称和描述中看到所有数据的原因 . 取而代之的是你应该使用data['name']
和描述data['description']
. 方括号中的字符串是firebase数据库中的键 .另一个建议是在 one
StreamBuilder
中显示所有用户数据,因为您不会两次下载数据 . 在这样一个简单的例子中它并不重要,但是当你开始处理更大的数据时,这可能会影响加载时间 .这是
_ProfileCardState
的修订版本(希望)对您有用 . 如果您还有其他问题,请与我们联系 .这是一张图片:
早上好
我的方法与你悄然不同 . 我总是写一个类来从数据源获取数据然后我在每个地方使用它 . 我认为你是这个框架的新手,所以我将按步骤解释
获取数据类是:
正如你所说,你想要男性的所有物品 . Firebase数据库会为您提供一个 Map 列表(Map是一个字典) .
我会再次重写你的Males对象
现在你把它放在你的视野中
now you have the full list from firebase and their data, and you can display them all