尝试添加提供程序以自动更新来自 firestore 的新用户信息



我制作了一个简单的个人资料页面,显示用户的个人资料图片,姓名和统计数据(全部来自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))
],
));
}

我还创建了一个新的 user_model.dart 文件,如下所示:

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);
}
}

我尝试使用各种方法,包括标准提供程序和流提供程序。

我收到运行时错误:"包:提供程序/src/delegate_widget.dart":断言失败:第 228 行 pos 16:"_builder !=null":不正确。

您的UserModel构建函数没有返回 Widget。

相关内容

最新更新