简体   繁体   English

如何在我的 Flutter 应用程序中使用我的数据模型?

[英]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.

相关问题 当我的 Flutter 应用程序关闭时,如何更新我的 Firebase 数据? - How can I update my Firebase data when my flutter app closes? 如何在我的 flutter firebase 应用程序中检查用户名可用性 - How can i check username availability in my flutter firebase app 如何让我的 Firebase 应用程序使用数据库 - How Can I Have my Firebase App use the Database 当我尝试在我的 flutter 应用程序中使用 firebase 时,它会产生错误 - When I try to use firebase in my flutter app it produces an error 如何在我的 ios 应用程序中检索嵌套的 firebase 数据? - How can I retrieve nested firebase data in my ios app? 我应该如何在我的 Flutter 应用程序中使用 FirebaseAuth.instance.onAuthStateChanged? - How am I supposed to use FirebaseAuth.instance.onAuthStateChanged in my flutter app? 我无法将 Firebase 数据检索到我的颤振应用程序 - I cant retrieve firebase data to my flutter app Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check? 如何在Firebase中为数据建模? - How should I model my data in Firebase? 如何覆盖我的主要 pubspec 依赖项(对 flutter 应用程序进行白标) - How can I override my main pubspec dependencies (white labelling a flutter app)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM