database structure screenshot我正在构建配置文件屏幕,并尝试使用streambuilder从Firestore中的数据库中提取每个用户的名称。但是,我的代码似乎不是从firestore数据库中提取的,并且我仍然得到与以前硬编码相同的名称(每次运行代码时)。下面是我的代码(实际代码中没有错误)。如何修改我的代码,以便实际上可以从Firestore数据库中读取代码?
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:ui' as ui;
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Profile Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Profile'),
);
}
}
class ProfileWidget extends StatelessWidget {
final String userId;
ProfileWidget(this.userId);
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream:
Firestore.instance.collection('users').document(userId).snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
debugPrint('the snapshot name is:'+ snapshot.data['name']);
User user = User.fromSnapshot(snapshot.data);
return Row(
children: <Widget>[Text(snapshot.data['name'].toString())]);
} else {
return CircularProgressIndicator();
}
});
}
}
class User {
final int name;
final DocumentReference reference;
User.fromMap(Map<String, dynamic> map, {this.reference}) : name = map['name'];
User.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final _width = MediaQuery.of(context).size.width;
final _height = MediaQuery.of(context).size.height;
final String imgUrl =
'https://pixel.nymag.com/imgs/daily/selectall/2017/12/26/26-eric-schmidt.w700.h700.jpg';
return new Stack(
children: <Widget>[
new Container(
color: Colors.blue,
),
new Image.network(
imgUrl,
fit: BoxFit.fill,
),
new BackdropFilter(
filter: new ui.ImageFilter.blur(
sigmaX: 6.0,
sigmaY: 6.0,
),
child: new Container(
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.9),
borderRadius: BorderRadius.all(Radius.circular(50.0)),
),
)),
new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
centerTitle: false,
elevation: 0.0,
backgroundColor: Colors.transparent,
),
drawer: new Drawer(
child: new Container(),
),
backgroundColor: Colors.transparent,
body: new Center(
child: new Column(
children: <Widget>[
new SizedBox(
height: _height / 12,
),
new CircleAvatar(
radius: _width < _height ? _width / 4 : _height / 4,
backgroundImage: NetworkImage(imgUrl),
),
new SizedBox(
height: _height / 25.0,
),
new Text(
'Eric Schmidt',
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: _width / 15,
color: Colors.white),
),
new Padding(
padding: new EdgeInsets.only(
top: _height / 30, left: _width / 8, right: _width / 8),
child: new Text(
'Snowboarder, Superhero and writer.\nSometime I work at google as Executive Chairman ',
style: new TextStyle(
fontWeight: FontWeight.normal,
fontSize: _width / 25,
color: Colors.white),
textAlign: TextAlign.center,
),
),
new Divider(
height: _height / 30,
color: Colors.white,
),
new Row(
children: <Widget>[
rowCell(343, 'POSTS'),
rowCell(673826, 'FOLLOWERS'),
rowCell(275, 'FOLLOWING'),
],
),
new Divider(height: _height / 30, color: Colors.white),
new Padding(
padding: new EdgeInsets.only(
left: _width / 8, right: _width / 8),
child: new FlatButton(
onPressed: () {},
child: new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Icon(Icons.person),
new SizedBox(
width: _width / 30,
),
new Text('FOLLOW')
],
)),
color: Colors.blue[50],
),
),
],
),
))
],
);
}
Widget rowCell(int count, String type) => new Expanded(
child: new Column(
children: <Widget>[
new Text(
'$count',
style: new TextStyle(color: Colors.white),
),
new Text(type,
style: new TextStyle(
color: Colors.white, fontWeight: FontWeight.normal))
],
));
}