How to create a user profile based on uid and navigate to the profile page?

Issue

I created a user with firebase authentication, also it has its own database.Like this

The user can send friend requests to other users, the senders uid is saved in the receiver user’s request collection. In my app I can list the requests.

requests

So I have the user’s uid and when I press the eye icon I want to navigate to another page where will be the user’s profile. So I need something like UserProfilePage class. Here’s my code:

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();
          }
        },
      ),
    );
  }
}

I’m a beginner in Flutter. Thank you for your help.

Solution

You have to do another query, since queries in Firestore are shallow, meaning if you are pointing to the collection requests, you won’t get the details inside the collection users. So you have to get the user details and then send them to the UserProfilePage page:

onPressed: () async {
  DocumentSnapshot result =  await Firestore.instance.collection('users').document(uid).get();
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => UserProfilePage(userInfo : result)),
  );
},

Answered By – Peter Haddad

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.