Flutter 正在尝试添加提供程序以自动更新firestore中的新用户信息

Flutter 正在尝试添加提供程序以自动更新firestore中的新用户信息,flutter,flutter-provider,Flutter,Flutter Provider,我制作了一个简单的个人资料页面,其中显示了用户的个人资料图片、姓名和统计信息(全部来自firestore数据库)。我现在正在尝试实现提供程序,以便在将新集合添加到firestore数据库时,配置文件页面将自动更新。我在main.dart的顶部尝试了以下操作: import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'dart:ui' as ui; i

我制作了一个简单的个人资料页面,其中显示了用户的个人资料图片、姓名和统计信息(全部来自firestore数据库)。我现在正在尝试实现提供程序,以便在将新集合添加到firestore数据库时,配置文件页面将自动更新。我在main.dart的顶部尝试了以下操作:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:ui' as ui;
import 'package:provider/provider.dart';
import 'user_model.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<UserModel>.value(
          notifier: UserModel(),
        ),
      ],
      child: MaterialApp(
      title: 'Profile Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Profile'),
    ),
    );
  }
}

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 Photo {
  final int photourl;
  final DocumentReference reference;

  Photo.fromMap(Map<String, dynamic> map, {this.reference})
      : photourl = map['photourl'];

  Photo.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);
}

class Questions {
  final int totalquestions;
  final DocumentReference reference;

  Questions.fromMap(Map<String, dynamic> map, {this.reference})
      : totalquestions = map['totalquestions'];

  Questions.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;
    return StreamBuilder<DocumentSnapshot>(
        stream: Firestore.instance
            .collection('users')
            .document('testuser')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Stack(
              children: <Widget>[
                new Container(
                  color: Colors.blue,
                ),
                new Image.network(
                  snapshot.data['photourl'].toString(),
                  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(snapshot.data['photourl']),
                          ),
                          new SizedBox(
                            height: _height / 25.0,
                          ),
                          new Text(
                            snapshot.data['name'],
                            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),
                          ),
                          new Divider(
                            height: _height / 15,
                            color: Colors.white,
                          ),
                          new Row(
                            children: <Widget>[
                              rowCell(
                                  snapshot.data['totalquestions'], 'Answers'),
                              rowCell(
                                  '£ ${int.parse(snapshot.data['totalquestions']) * 2}', 'Earned'),
                            ],
                          ),
                          new Divider(
                              height: _height / 15, color: Colors.white),
                        ],
                      ),
                    ))
              ],
            );
          } else {
            return CircularProgressIndicator();
          }
        });
  }

  Widget rowCell(String 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))
        ],
      ));
}
导入“包装:颤振/材料.省道”;
导入“包:cloud_firestore/cloud_firestore.dart”;
将“dart:ui”导入为ui;
导入“包:provider/provider.dart”;
导入“user_model.dart”;
void main()=>runApp(新的MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
回程多供应商(
供应商:[
ChangeNotifierProvider.value(
通知程序:UserModel(),
),
],
孩子:MaterialApp(
标题:“配置文件演示”,
主题:新主题数据(
主样本:颜色。蓝色,
),
主页:新MyHomePage(标题:“个人资料”),
),
);
}
}
类用户{
最终整数名;
最终文件参考;
User.fromMap(映射映射,{this.reference})
:name=map['name'];
User.fromSnapshot(DocumentSnapshot快照)
:this.fromMap(snapshot.data,reference:snapshot.reference);
}
班级照片{
最终int photourl;
最终文件参考;
Photo.fromMap(映射映射,{this.reference})
:photourl=map['photourl'];
Photo.fromSnapshot(文档快照快照)
:this.fromMap(snapshot.data,reference:snapshot.reference);
}
课堂提问{
最后问题;
最终文件参考;
problems.fromMap(Map-Map,{this.reference})
:totalquestions=map['totalquestions'];
问题。来自快照(文档快照快照)
:this.fromMap(snapshot.data,reference:snapshot.reference);
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
final _width=MediaQuery.of(context).size.width;
final _height=MediaQuery.of(context).size.height;
返回流生成器(
流:Firestore.instance
.collection('用户')
.document('testuser'))
.snapshots(),
生成器:(上下文,快照){
if(snapshot.hasData){
返回堆栈(
儿童:[
新容器(
颜色:颜色,蓝色,
),
新图像网络(
snapshot.data['photourl'].toString(),
fit:BoxFit.fill,
),
新型背向滤波器(
过滤器:新建ui.ImageFilter.blur(
sigmaX:6.0,
西格梅:6.0,
),
子容器:新容器(
装饰:盒子装饰(
颜色:颜色。蓝色。不透明度(0.9),
borderRadius:borderRadius.all(半径.圆形(50.0)),
),
)),
新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
标题:错误,
标高:0.0,
背景颜色:颜色。透明,
),
抽屉:新抽屉(
子级:新容器(),
),
背景颜色:颜色。透明,
正文:新中心(
子:新列(
儿童:[
新尺寸盒子(
高度:_高度/12,
),
新圆星(
半径:_宽度<_高度?_宽度/4:_高度/4,
背景图片:NetworkImage(snapshot.data['photourl']),
),
新尺寸盒子(
高度:_高度/25.0,
),
新文本(
snapshot.data['name'],
样式:新文本样式(
fontWeight:fontWeight.bold,
字体大小:_宽度/15,
颜色:颜色。白色),
),
新填料(
填充:仅限新边设置(
顶部:_高度/30,
左:_宽度/8,
右:_宽度/8),
),
新分隔器(
高度:_高度/15,
颜色:颜色,白色,
),
新行(
儿童:[
罗塞尔(
snapshot.data['totalquestions','Answers'),
罗塞尔(
“£${int.parse(snapshot.data['totalquestions'])*2}”,“挣来的”,
],
),
新分隔器(
高度:_高度/15,颜色:颜色。白色),
],
),
))
],
);
}否则{
返回循环ProgressIndicator();
}
});
}
小部件行单元格(字符串计数,字符串类型)=>新扩展(
子:新列(
儿童:[
新文本(
“$count”,
样式:新文本样式(颜色:Colors.white),
)
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class UserModel extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final UserModel userModel = Provider.of<UserModel>(context);
  }
}