0

I am building a profile page. But I am having some issues in retrieving name and mail from the firebase.

 firebase_auth: ^3.1.1
 firebase_core: ^1.6.0
 google_sign_in: ^5.1.0
 firebase_storage: ^10.0.3
 cloud_firestore: ^2.5.3
 firebase_analytics: ^8.3.2
 lottie: ^1.1.0
 image_picker: ^0.8.4

Above is my pubspec.yaml dependencies.

  Widget headerProfile(
  BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
  return SizedBox(
  height: MediaQuery.of(context).size.height * 0.26,
  width: MediaQuery.of(context).size.width,
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      Container(
        height: 150.0,
        width: 170.0,
        child: Column(
          children: [
            GestureDetector(
              onTap: () {},
              child: CircleAvatar(
                backgroundColor: constantColors.transparent,
                radius: 38.0,
                backgroundImage: NetworkImage(
                    '${Provider.of<FirebaseOperations>(context).getInitUserImage}'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Text(
                snapshot.data()['username'],
                style: TextStyle(
                    color: constantColors.whiteColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0),
              ),
            ),

Above is the profilehelpers.dart page.

 class Profile extends StatefulWidget {
 @override
 _ProfileState createState() => _ProfileState();
 }

class _ProfileState extends State<Profile> {
final ConstantColors constantColors = ConstantColors();
 @override
 Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    centerTitle: true,
    leading: IconButton(
      onPressed: () {},
      icon: Icon(EvaIcons.settings2Outline,
          color: constantColors.lightBlueColor),
    ),
    actions: [
      IconButton(
          icon: Icon(EvaIcons.logOutOutline,
              color: constantColors.greenColor),
          onPressed: () {
            Provider.of<ProfileHelpers>(context, listen: false)
                .logOutDialog(context);
          })
    ],
    backgroundColor: constantColors.blueGreyColor.withOpacity(0.4),
    title: RichText(
      text: TextSpan(
          text: 'My ',
          style: TextStyle(
            color: constantColors.whiteColor,
            fontWeight: FontWeight.bold,
            fontSize: 20.0,
          ),
          children: <TextSpan>[
            TextSpan(
                text: 'Profile',
                style: TextStyle(
                  color: constantColors.blueColor,
                  fontWeight: FontWeight.bold,
                  fontSize: 20.0,
                ))
          ]),
       ),
      ),
    body: SingleChildScrollView(
    child: Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
       
        child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
          stream: FirebaseFirestore.instance
              .collection('users')
              .doc(Provider.of<Authentication>(context, listen: false)
                  .getUserUid)
              .snapshots(),
          builder: (BuildContext context,
              AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
              );
            } else {
              return new Column(
                children: [
                  Provider.of<ProfileHelpers>(context, listen: false)
                      .headerProfile(context, snapshot),
                  Provider.of<ProfileHelpers>(context, listen: false)
                      .divider(),
                  Provider.of<ProfileHelpers>(context, listen: false)
                      .middleProfile(context, snapshot),
                  Provider.of<ProfileHelpers>(context, listen: false)
                      .footerProfile(context, snapshot)
                ],
              );
            }
          },
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15.0),
            color: constantColors.blueGreyColor.withOpacity(0.6)),
      ),
    ),
     ),
    );
    }
    }

Above is the profile.dart file code. I think the problem is somewhere with document and asyncsnapshot and 'snapshot.data()['username']'.If someone can point out my mistake it will be more helpful for me.

40:34: Error: 'data' isn't a function or method and can't be 
  invoked.
                snapshot.data()['username'],
                             ^^^^


FAILURE: Build failed with an exception.

This is the error I am getting

Aswin B
  • 47
  • 7
  • please also post your console after run. it would be helpful to find the problem – Taqi Tahmid Tanzil Sep 18 '21 at 05:06
  • Sorry for the inconvenience. I have added the error status above. – Aswin B Sep 18 '21 at 05:19
  • Try This, From : snapshot.data()['username'], To : snapshot.data['username'], Check This : https://stackoverflow.com/questions/64792658/error-currentuser-isnt-a-function-or-method-and-cant-be-invoked – Yashraj Sep 18 '21 at 05:31
  • snapshot.data['username'], didn't work,as it showing 'The method '[]' can't be unconditionally invoked because the receiver can be 'null'. ' – Aswin B Sep 18 '21 at 05:49

2 Answers2

0

Should be snapshot.data.data()['username'] not snapshot.data()['username'];

You may still get errors (like Object not being a subtype of Map) because of the way you wrote your code, in that case, use this (copy and paste into your code, I changed some things):

Widget headerProfile(
  BuildContext context, AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
  Map<String, dynamic>? map = snapshot.data!.data();
  return SizedBox(
  height: MediaQuery.of(context).size.height * 0.26,
  width: MediaQuery.of(context).size.width,
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      Container(
        height: 150.0,
        width: 170.0,
        child: Column(
          children: [
            GestureDetector(
              onTap: () {},
              child: CircleAvatar(
                backgroundColor: constantColors.transparent,
                radius: 38.0,
                backgroundImage: NetworkImage(
                    '${Provider.of<FirebaseOperations>(context).getInitUserImage}'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Text(
                map['username'],
                style: TextStyle(
                    color: constantColors.whiteColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0),
              ),
            ),
Peter Obiechina
  • 2,686
  • 1
  • 7
  • 14
0

can you please try like "snapshot.data['username']"??

also refer to this post link for fetching data from real time firebase DB

  • snapshot.data['username'], didn't work,as it showing 'The method '[]' can't be unconditionally invoked because the receiver can be 'null'. – Aswin B Sep 18 '21 at 06:09