如何基于uid创建用户个人资料并导航至个人资料页面?

时间:2020-08-27 06:04:56

标签: firebase flutter dart google-cloud-firestore flutter-navigation

我创建了一个具有Firebase身份验证的用户,它也拥有自己的数据库。Like this

用户可以向其他用户发送好友请求,发件人uid保存在接收用户的请求集合中。在我的应用中,我可以列出请求。

requests

所以我有用户的uid,当我按下眼睛图标时,我想导航到另一个页面,该页面将是用户的个人资料。所以我需要类似UserProfilePage类的东西。这是我的代码:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_auths/pages/profilepage.dart';
import 'package:flutter_auths/services/authentications.dart';
import 'package:flutter_auths/main.dart';


class RequestPage extends StatefulWidget {
  final String uid;

  RequestPage({Key key, @required this.uid}) : super(key: key);

  @override
  _RequestPageState createState() => _RequestPageState(uid);
}

class _RequestPageState extends State<RequestPage> {
  final String uid;
  _RequestPageState(this.uid);

  var taskcollections = Firestore.instance.collection('users');
  String task;


  void acceptFriendRequest(String theUid) async {
    await Firestore.instance
        .collection('users')
        .document(uid)
        .collection('friendships')
        .document()
        .setData({
      'FriendUid': theUid,
    }).then((onValue) {
      print('current user: ');
      print(uid);
      print(theUid);
    });

    await Firestore.instance
        .collection('users')
        .document(theUid)
        .collection('friendships')
        .document()
        .setData({
      'FriendUid': uid,
    }).then((onValue) {
    });

    await Firestore.instance
        .collection('users')
        .document(uid)
        .collection('requests')
        .document(theUid)
        .delete();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {  },
        child: Icon(Icons.add),
      ),
      appBar: AppBar(
        title: Text(
          "Requests",
          style: TextStyle(
            fontFamily: "tepeno",
            fontWeight: FontWeight.w600,
          ),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.exit_to_app),
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
            onPressed: () => signOutUser().then((value) {
              Navigator.of(context).pushAndRemoveUntil(
                  MaterialPageRoute(builder: (context) => HomePage()),
                      (Route<dynamic> route) => false);
            }),
          ),
        ],
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: taskcollections
            .document(uid)
            .collection('requests')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                DocumentSnapshot ds = snapshot.data.documents[index];
                return Container(
                  decoration: BoxDecoration(
                    color: Colors.blue,
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  margin: EdgeInsets.all(8.0),
                  child: ListTile(
                    title: Text(
                      ds['FriendUid'] ?? '',
                      style: TextStyle(
                        fontFamily: "tepeno",
                        fontSize: 18.0,
                        color: Colors.white,
                      ),
                    ),
                    trailing: Wrap(
                      spacing: 7, // space between two icons
                      children: <Widget>[
                        IconButton(
                          icon: Icon(
                            Icons.check,
                            size: 27.0,
                            color: Colors.brown[900],
                          ),
                          onPressed: () {
                            acceptFriendRequest(ds['FriendUid']);
                          },
                        ),
                        IconButton(
                          icon: Icon(
                            Icons.do_not_disturb,
                            size: 27.0,
                            color: Colors.brown[900],
                          ),
                          onPressed: () {
                            //   _onDeleteItemPressed(index);
                          },
                        ),
                        IconButton(
                          icon: Icon(
                            Icons.remove_red_eye,
                            size: 27.0,
                            color: Colors.brown[900],
                          ),
                          onPressed: () {


                          },
                        ),
                      ],
                    ),

                  ),
                );
              },
            );
          } else if (snapshot.hasError) {
            return CircularProgressIndicator();
          } else {
            return CircularProgressIndicator();
          }
        },
      ),
    );
  }
}

我是Flutter的初学者。谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您必须执行另一个查询,因为Firestore中的查询很浅,这意味着如果您指向集合users,则不会在集合UserProfilePage中获得详细信息。因此,您必须获取用户详细信息,然后将其发送到onPressed: () async { DocumentSnapshot result = await Firestore.instance.collection('users').document(uid).get(); Navigator.push( context, MaterialPageRoute(builder: (context) => UserProfilePage(userInfo : result)), ); }, 页面:

@Controller
@Scope("request")