我的窗口小部件没有出现在屏幕上

时间:2020-10-25 14:44:26

标签: flutter dart

在我的代码中,我制作了一个小部件buildProfilePosts(),并在我的Scaffold中使用了它。但是,当我运行该应用程序时,屏幕上没有显示该小部件的内容。有人能帮助我吗。我没有收到任何错误或警告,但仍然没有输出。我已经制作了一个函数getProfilePosts()来将值放入列表中。我想确认我的问题出在哪一部分上,在获取列表/帖子或显示它们上以及为什么????。请有人帮忙。

这是我的代码-

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:social_app/models/users.dart';
import 'package:social_app/pages/edit_profile.dart';
import 'package:social_app/pages/home.dart';
import 'package:social_app/widgets/header2.dart';
import 'package:social_app/widgets/post.dart';
import 'package:social_app/widgets/progress.dart';

String headOfProfilePage;

class Profile extends StatefulWidget {

  final String profileId;
  Profile({this.profileId});

  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
  @override
  void initState() {
    getProfilePosts();
    super.initState();
  }
  bool isLoading = false;
  int postsCount = 0;
  List<Post> posts = [];

  getProfilePosts() async {
    setState(() {
      isLoading = true;
    });
    QuerySnapshot snapshot = await usersRef.doc(widget.profileId).collection('userPosts').orderBy('timeStamp', descending: true).get();
    setState(() {
      isLoading = false;
      postsCount = snapshot.docs.length;
      posts = snapshot.docs.map((doc) => Post.fromDocument(doc)).toList();
    });
  }

  final String currentUserId = currentUser?.id;

  buildCountColumn(String label, int count) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          count.toString(),
          style: TextStyle(
            color: Colors.white,
            fontSize: 18.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 4.0),
          child: Text(
            label,
            style: TextStyle(
              color: Colors.white,
              fontSize: 15.0,
              fontWeight: FontWeight.w400,
            ),
          ),
        ),
      ],
    );
  }

  editProfile() {
    Navigator.push(context, MaterialPageRoute(builder: (context) => EditProfile(currentUserId: currentUserId)));
  }

  Container buildButton({String text, Function function}) {
    return Container(
      padding: EdgeInsets.only(top: 2.0),
      child: FlatButton(
        onPressed: function,
        child: Container(
          margin: EdgeInsets.symmetric(vertical: 10.0),
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.width*0.95/12.8,
          alignment: Alignment.center,
          child: Text(
            text,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w500,
            ),
          ),
          decoration: BoxDecoration(
            color: text == "Edit Profile" ? Colors.black : Colors.blue,
            border: Border.all(
              color: text == "Edit Profile" ? Colors.white : Colors.blue,
            ),
            borderRadius: BorderRadius.circular(5.0),
          ),
        ),
      ),
    );
  }

  buildProfileButton() {
    bool isProfileOwner = currentUserId == widget.profileId;
    if(isProfileOwner) {
      return buildButton(
        text: "Edit Profile",
        function: editProfile,
      );
    }
  }

  buildProfileHeader() {
    return FutureBuilder(
      future: usersRef.doc(widget.profileId).get(),
      builder: (context, snapshot) {
        if(!snapshot.hasData) {
          return circularProgress();
        }
        User user = User.fromDocument(snapshot.data);

        @override
        void initState() async {
          headOfProfilePage = user.username;
          super.initState();
        }
        return Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  CircleAvatar(
                    radius: 40.0,
                    backgroundColor: Colors.grey,
                    backgroundImage: CachedNetworkImageProvider(user.photoUrl),
                  ),
                  Expanded(
                    flex: 1,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      mainAxisSize: MainAxisSize.max,
                      children: <Widget>[
                        buildCountColumn("Posts", 0),
                        buildCountColumn("Followers", 0),
                        buildCountColumn("Following", 0),
                      ],
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                ],
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 12.0),
                child: Text(
                  user.username,
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                    fontSize: 16.0,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 4.0),
                child: Text(
                  user.displayName,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.0,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 2.0),
                child: Text(
                  user.bio,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 13.0,
                    //fontWeight: FontWeight.w400,
                  ),
                ),
              ),
              buildProfileButton(),
            ],
          ),
        );
      },
    );
  }

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

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Color(0xff111111),
        appBar: header2(context, titleText: 'Profile'),
        body: ListView(
          children: <Widget>[
            buildProfileHeader(),
            Divider(
              height: 0.0,
            ),
            buildProfilePosts(),
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

为什么要在buildProfileHeader()中声明初始化状态?

那可能是原因,请检查一下。

答案 1 :(得分:0)

您要声明initState 2次,当对象插入树中时,它会被Flutter框架调用。我认为拥有2 initState框架无法完成其状态初始化并进入无限循环。如果要更改状态,请尝试使用setState方法。同样,initState方法也不能是async

尝试在initState内替换buildProfileHeader方法

@override
void initState() async {
  headOfProfilePage = user.username;
  super.initState();
}

跟随

void settingState() {
  setState(() {
    headOfProfilePage = user.name;
  })
}

或者如果您想在调用builder时更改状态,请尝试设置状态而无需像下面的setState那样调用需要的功能:

setState(() {
  headOfProfilePage = user.username;
})

此外,您没有在函数内使用await,因此不需要async并调用setState会重新加载小部件,因此您无需调用initSate窗口小部件初始化后。