[英]How can I use my data model in my Flutter app?
I am new to this so I do apologize.我是新手,所以我很抱歉。 But I created a Firebase Firestore data model to represent my article post from Firestore.
但是我创建了一个 Firebase Firestore 数据模型来表示我在 Firestore 中的文章帖子。 When I type it into the AppBar's title section, I am getting an error.
当我将其输入到 AppBar 的标题部分时,出现错误。 How do you work with this?
你如何处理这个问题? I would appreciate any sort of help or hints as I am new to this so I am still learning.
我很感激任何形式的帮助或提示,因为我是新手,所以我仍在学习。 I have also downloaded all the required dependencies for this.
我还为此下载了所有必需的依赖项。
My Firestore database screen in case if this is also needed我的 Firestore 数据库屏幕,以防万一这也需要
This is my Post model which models my articles from my Firestore database.这是我的 Post 模型,它为我的 Firestore 数据库中的文章建模。
class Post {
String title;
String imageURL;
String text;
Post({this.title, this.imageURL, this.text});
factory Post.fromFirestore(DocumentSnapshot doc) {
var data = doc.data();
return Post(
title: data['title'],
imageURL: data['imageURL'],
text: data['text']
);
}
}
This is my article page (ReaderPage)这是我的文章页面 (ReaderPage)
// This page displays article to the user.
class ReaderPage extends StatelessWidget {
CollectionReference posts = FirebaseFirestore.instance.collection('posts');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// This line is causing errors...
title: Post.fromFirestore().title,
),
body: Container(
),
);
}
}
The model is correct, but you are not reading from Firestore.该模型是正确的,但您没有从 Firestore 读取数据。 You could use a FutureBuilder to build the content only when the promise given by the firestore query has been resolved:
只有当 firestore 查询给出的承诺得到解决时,您才能使用 FutureBuilder 来构建内容:
return FutureBuilder<DocumentSnapshot>(
future: posts.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data.data();
return return Scaffold(
appBar: AppBar(
title: data.title,
),
body: Container(
),
);
}
return Text("loading");
},
);
But in this way you are not using the model you have created.但是通过这种方式,您并没有使用您创建的模型。
In my opinion the best way could be implement a DatabaseService class:在我看来,最好的方法是实现一个 DatabaseService 类:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:async';
import 'posts.dart';
class DatabaseService {
final Firestore _db = Firestore.instance;
/// Get a stream of a single document
Stream<Post> streamPost(String uid) {
return _db
.collection('posts')
.document(uid)
.snapshots()
.map((snap) => Post.fromFirestore(snap.data));
}
/// Query a subcollection
Stream<List<Post>> streamPost(String uid) {
var ref = _db.collection('posts');
return ref.snapshots().map((list) =>
list.documents.map((doc) => Post.fromFirestore(doc)).toList());
}
}
But in this way you have to work with streams.但是通过这种方式,您必须使用流。 I suggest you to have a look at this library: Flutter Provider And this article could help you: Here
我建议你看看这个库: Flutter Provider这篇文章可以帮助你:这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.