Flutter Firebase屏幕上没有呈现任何内容,编译器也未发现错误

时间:2020-02-09 06:27:05

标签: firebase flutter dart

我不熟悉flutter和stackoverflow。我正在使用Firebase在Flutter上构建instagram克隆。并将用户的数据存储到firebase。但是现在,用户的帖子未加载They are supposed to load in this area

下面是代码,其中Post.fromDocumnet是一种工厂方法,用于存储用户的帖子数据

getProfilePost() async {
    setState(() {
      isLoading = true;
    });
    QuerySnapshot snapshot = await postRef
        .document(widget.profileId)
        .collection('userPosts')
        .orderBy('timestamp', descending: true)
        .getDocuments();
    setState(() {
      isLoading = false;
      postCount = snapshot.documents.length;
      print(postCount);
      print(isLoading);
      posts = snapshot.documents.map((doc) => Post.fromDocument(doc)).toList();
    });
  }

  buildProfilePosts() {
    if (isLoading) {
      return circularProgress();
    }
    return Column(
      children: posts,
    );
  }

这是整个帖子文件。

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:fluttershare/pages/home.dart';
import 'package:fluttershare/widgets/progress.dart';
import 'package:fluttershare/models/user.dart';

//actually a model like user model class but written in the same file with post widget so that we can add methods to it to pass them to our state class
class Post extends StatefulWidget {
  final String postId;
  final String ownerId;
  final String username;
  final String location;
  final String description;
  final String mediaUrl;
  final dynamic likes;
  Post({
    this.postId,
    this.ownerId,
    this.username,
    this.location,
    this.description,
    this.mediaUrl,
    this.likes,
  });
  factory Post.fromDocument(DocumentSnapshot doc) {
    return Post(
      postId: doc["postId"],
      ownerId: doc["ownerId"],
      username: doc["username"],
      description: doc["description"],
      location: doc["location"],
      mediaUrl: doc["mediaUrl"],
      likes: doc["likes"],
    );
  }
  int getLikeCount(likes) {
    if (likes == null) {
      return 0;
    }
    int count = 0;
    likes.values.forEach((val) {
      if (val) {
        count += 1;
      }
    });
    return count;
  }

  @override
  _PostState createState() => _PostState(
        postId: this.postId,
        ownerId: this.ownerId,
        username: this.username,
        location: this.location,
        description: this.description,
        mediaUrl: this.mediaUrl,
        likes: this.likes,
        likeCount: getLikeCount(this.likes),
      );
}

class _PostState extends State<Post> {
  final String postId;
  final String ownerId;
  final String username;
  final String location;
  final String description;
  final String mediaUrl;
  int likeCount;
  Map likes;
  _PostState({
    this.postId,
    this.ownerId,
    this.username,
    this.location,
    this.description,
    this.mediaUrl,
    this.likes,
    this.likeCount,
  });
  buildPostHeader() {
    return FutureBuilder(
        future: userRef.document(ownerId).get(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return circularProgress();
          }
          User user = User.fromDocument(snapshot.data);
          return ListTile(
            leading: GestureDetector(
              onTap: () => print("circle avatar tapped"),
              child: CircleAvatar(
                backgroundImage: CachedNetworkImageProvider(user.photoURL),
                backgroundColor: Colors.grey,
              ),
            ),
            title: GestureDetector(
              onTap: () => print("Tapped on text title"),
              child: Text(
                user.username,
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            subtitle: Text(location),
            trailing: IconButton(
                icon: Icon(Icons.more_vert),
                onPressed: () => print("Pressed on trailing icon button")),
          );
        });
  }

  buildPostImage() {
    return GestureDetector(
      onDoubleTap: () => print("double Tapped"),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Image.network(mediaUrl),
        ],
      ),
    );
  }

  buildPostFooter() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Row(
          children: <Widget>[
            GestureDetector(
              onTap: () => print('liked'),
              child: Icon(
                Icons.favorite_border,
                color: Colors.pink,
              ),
            ),
            GestureDetector(
              onTap: () => print('comment'),
              child: Icon(
                Icons.chat,
                color: Colors.blue,
              ),
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 20.0),
              child: Text(
                "$likeCount likes",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                ),
              ),
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 20.0),
              child: Text(
                "$username",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                ),
              ),
            ),
          ],
        ),
        Expanded(child: Text(description)),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        buildPostHeader(),
        buildPostImage(),
        buildPostFooter(),
      ],
    );
  }
}

最后在配置文件类中以如下方式调用buildProfilePosts()

@override
  Scaffold build(BuildContext context) {
    return Scaffold(
        appBar: header(context, isAppTitle: false, titleText: "Profile"),
        body: ListView(
          children: <Widget>[
            buildProfileHeader(),
            Divider(
              height: 0.0,
            ),
            buildProfilePosts()
          ],
        ));
  }
}

但是屏幕上没有任何变化有任何帮助吗? 即使编译器没有显示此代码,这是怎么回事。 帖子的数据是在Firebase as shown here

中创建的

0 个答案:

没有答案