Saya memiliki koleksi user_profile di konsol aplikasi saya. Saya ingin mengambil dokumen (profil pengguna tertentu dengan id penggunanya [klik untuk memeriksa][1]).

Saya tahu bahwa FirebaseAuth.instance.currentUser; akan memberi saya ID pengguna login saat ini tetapi bukan itu yang saya inginkan. Saya ingin menunjukkan detail pengguna yang diklik, bukan pengguna yang masuk, sepertinya tidak dapat menemukan jawaban apa pun di sini yang membantu. Tolong bantu guys

Ini adalah metode yang mendapatkan koleksi

  Future<DocumentSnapshot> getUserData() {
    var firebaseUser = FirebaseAuth.instance.currentUser;
    _firestoreInstance
        .collection('user_profile')
        .doc(firebaseUser.uid)
        .get()
        .then((value) {
      print(value.data());
      return value.data();
    });
  }

Dan inilah pembangun masa depan saya

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: getUserData(),
      // ignore: missing_return
      builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {

        if (snapshot.hasError) {
          return Text('Error fetching user profile');
        }

        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> userData = snapshot.data.data();
          return Scaffold(
              body: SafeArea(
            child: Container(
              padding: EdgeInsets.only(top: 10),
              child: Column(
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: IconButton(
                      onPressed: () {
                        // Navigator.pushNamed(context, Homepage.id);
                      },
                      icon: Icon(
                        Icons.arrow_back,
                        color: Colors.white,
                        size: 30,
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: () {},
                    child: Stack(
                      children: [
                        CircleAvatar(
                          radius: 70,
                          backgroundColor: Colors.transparent,
                          child: ClipOval(
                            child: Image.asset('assets/avatar_profile.jpg'),
                          ),
                        ),
                        Positioned(
                          bottom: 0,
                          right: 0,
                          child: CircleAvatar(
                            backgroundColor: Colors.white60,
                            radius: 25,
                            child: IconButton(
                              onPressed: () {},
                              icon: Icon(Icons.edit, color: Colors.blueGrey),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  IntrinsicHeight(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          "${userData['nickname']}",
                          style: TextStyle(
                              color: Colors.white, fontWeight: FontWeight.w900),
                        ),
                        VerticalDivider(
                          thickness: 3,
                          width: 20,
                          color: Colors.white,
                        ),
                        Text(
                          // '7',
                          "{$userData['age]}",
                          style: TextStyle(
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(20.0),
                    child: Card(
                      child: Column(
                        children: [
                          ListTile(
                            leading: Icon(
                              Icons.person,
                              size: 40,
                            ),
                            title: Text("About me"),
                            isThreeLine: false,
                            dense: true,
                            subtitle: Text("${userData['aboutMe']}"),
                            trailing: Icon(Icons.arrow_right),
                          )
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      width: 400,
                      child: ListView(
                        children: [
                          ProfileListTile(
                            leading: Icons.phone_in_talk,
                            title: 'Phone Number',
                            subtitle: "${userData['mobile']}",
                          ),
                          ProfileListTile(
                            leading: Icons.add_location,
                            title: 'Current Location',
                            subtitle: "${userData['location']}",
                          ),
                          ProfileListTile(
                            leading: FontAwesomeIcons.heartbeat,
                            title: 'Relationship Status',
                            subtitle: "${userData['maritalStatus']}",
                          ),
                          ProfileListTile(
                            leading: Icons.people,
                            title: 'Gender',
                            subtitle: 'Male',
                          ),
                          ProfileListTile(
                            leading: Icons.looks,
                            title: 'Interested In',
                            subtitle: "${userData['InterestedIn']}",
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ));
        }
      },
    );
  }
}
0
Dave Davoucii 9 Januari 2021, 00:34

3 jawaban

Jawaban Terbaik

Saya dapat menyelesaikan ini dengan mudah hanya dengan meneruskan dari UsersCard ke UserProfile (buruk, lolx), karena saya sudah memiliki data ini di layar daftar pengguna, tidak perlu mengambilnya lagi dengan FutureBuilder

class UsersCard extends StatelessWidget {
  final Users usersDetails;

  const UsersCard({Key key, @required this.usersDetails}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(builder: (_) => 
         UserProfile(userDetails: usersDetails))); //im passing the data here
        
      },
      child: Stack(
        children: [
          Card(
            semanticContainer: true,
            clipBehavior: Clip.antiAliasWithSaveLayer,
            child: Image.network(
              'https://placeimg.com/170/170/any',
              fit: BoxFit.contain,
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
            ),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10.0),
            ),
            elevation: 5,
          ),
          Positioned(
            left: 20,
            bottom: 20,
            child: IntrinsicHeight(
              child: Row(
                children: [
                  Text(
                    '${usersDetails.nickname}',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.w900),
                  ),
                  VerticalDivider(
                    thickness: 2,
                    width: 20,
                    color: Colors.white,
                  ),
                  Text(
                    '${usersDetails.age}',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.w900),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Ini Layar Profil Pengguna saya

class UserProfile extends StatelessWidget {
  final Users userDetails;

  UserProfile({@required this.userDetails});

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: SafeArea(
        child: Container(
          padding: EdgeInsets.only(top: 10),
          child: Column(
            children: [
              Align(
                alignment: Alignment.centerLeft,
                child: IconButton(
                  onPressed: () {
                    // Navigator.pushNamed(context, Homepage.id);
                  },
                  icon: Icon(
                    Icons.arrow_back,
                    color: Colors.white,
                    size: 30,
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {},
                child: Stack(
                  children: [
                    CircleAvatar(
                      radius: 70,
                      backgroundColor: Colors.transparent,
                      child: ClipOval(
                        child: Image.asset('assets/avatar_profile.jpg'),
                      ),
                    ),
                    Positioned(
                      bottom: 0,
                      right: 0,
                      child: CircleAvatar(
                        backgroundColor: Colors.white60,
                        radius: 25,
                        child: IconButton(
                          onPressed: () {},
                          icon: Icon(Icons.edit, color: Colors.blueGrey),
                        ),
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 10,
              ),
              IntrinsicHeight(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "${userDetails.nickname}",
                      style: TextStyle(
                          color: Colors.white, fontWeight: FontWeight.w900),
                    ),
                    VerticalDivider(
                      thickness: 3,
                      width: 20,
                      color: Colors.white,
                    ),
                    Text(
                      // '7',
                      "{$userDetails.age}",
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: Card(
                  child: Column(
                    children: [
                      ListTile(
                        leading: Icon(
                          Icons.person,
                          size: 40,
                        ),
                        title: Text("About me"),
                        isThreeLine: false,
                        dense: true,
                        subtitle: Text("${userDetails.aboutMe}"),
                        trailing: Icon(Icons.arrow_right),
                      )
                    ],
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  width: 400,
                  child: ListView(
                    children: [
                      ProfileListTile(
                        leading: Icons.phone_in_talk,
                        title: 'Phone Number',
                        subtitle: "${userDetails.mobile}",
                      ),
                      ProfileListTile(
                        leading: Icons.add_location,
                        title: 'Current Location',
                        subtitle: "${userDetails.location}",
                      ),
                      ProfileListTile(
                        leading: FontAwesomeIcons.heartbeat,
                        title: 'Relationship Status',
                        subtitle: "${userDetails.maritalStatus}",
                      ),
                      ProfileListTile(
                        leading: Icons.people,
                        title: 'Gender',
                        subtitle: 'Male',
                      ),
                      ProfileListTile(
                        leading: Icons.looks,
                        title: 'Interested In',
                        subtitle: "${userDetails.interestedIn}",
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

0
Dave Davoucii 9 Januari 2021, 01:50

Kode Anda saat ini belum mengembalikan apa pun dari getUserData. Satu-satunya pengembalian yang Anda miliki adalah di dalam fungsi then, dan tidak lolos ke tingkat yang lebih tinggi.

Cara termudah untuk memperbaikinya adalah dengan menggunakan await:

  Future<DocumentSnapshot> getUserData() async {
    var firebaseUser = FirebaseAuth.instance.currentUser;
    var doc = await _firestoreInstance
        .collection('user_profile')
        .doc(firebaseUser.uid)
        .get()
    return doc.data();
  }
1
Frank van Puffelen 8 Januari 2021, 22:46

Sepertinya Anda ingin pengguna aplikasi Anda melihat profil orang lain.

Untuk melakukannya, pertama-tama Anda harus menerima uid pengguna untuk menampilkan profil. Kemudian Anda dapat meneruskan uid ke masa depan seperti contoh berikut:

class ProfilePage extends StatelessWidget {
  const ProfilePage({
    Key key,
    @required this.uid, // Here you are receiving the uid of the currently viewd user's uid
  }) : super(key: key);

  final String uid;

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Map<String, dynamic>>(builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
        return Center(child: CircularProgressIndicator());
      } else if (snapshot.hasError) {
        return Center(child: Text('Error fetching user profile'));
      }
      final userProfile = snapshot.data;
      return Column(
        children: [
          // Here you would paste the contents of your user profile widget
        ],
      );
    });
  }

  Future<Map<String, dynamic>> getUserData() async {
    final snap =
        await _firestoreInstance.collection('user_profile').doc(uid).get();
    return snap.data();
  }
}
0
dshukertjr 9 Januari 2021, 00:18