skip to Main Content

I am currently building an app in Flutter.

I was able to get the account name and email address from Firestore using the second code below.

I am having trouble getting them without some action (such as pressing a button).

I would like to display this as a Text Widget.

Is there a better way to do this?

Code to display the account name↓

class NavBar extends StatelessWidget {
  const NavBar({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => account()),
              );
            },
            child:
          UserAccountsDrawerHeader(
            accountName: Text('開発者',style: TextStyle(color: Colors.white),),
            accountEmail: Text('[email protected]',style: TextStyle(color: Colors.white),),
            currentAccountPicture: CircleAvatar(
              child: ClipOval(
                child: Image.network(
                  'https://pbs.twimg.com/profile_images/1494938183448281089/xXIv3xmE_400x400.jpg',
                  width: 90,
                  height: 90,
                  fit: BoxFit.cover,
                ),
              ),
            ),
            decoration: BoxDecoration(
              color: Colors.lightGreen,
              image: DecorationImage(
                image: NetworkImage(
                  'https://pbs.twimg.com/profile_banners/1394312681209749510/1634787753/1500x500',
                ),
                fit: BoxFit.cover,
              ),
            ),
          ),
          ),
          ListTile(
            leading: Icon(Icons.event_available_outlined),
            title: Text('行事予定'),
            onTap: () {
             launch('https://www.ous.ac.jp/common/files//285/20220311164731084854.pdf');
            },
          ),
          ListTile(
            leading: Icon(Icons.public_outlined),
            title: Text('マイログ'),
            onTap: () {
              launchUrl(Uri.https('mylog.pub.ous.ac.jp', '/uprx/up/pk/pky501/Pky50101.xhtml'),mode:LaunchMode.externalApplication );

            },
          ),
          ListTile(
            leading: Icon(Icons.book_outlined),
            title: Text('学生便覧'),
            onTap: () {
              launch('https://edu.career-tasu.jp/p/digital_pamph/frame.aspx?id=7540000-3-30&FL=0');
            },
          ),
          Divider(),

          ListTile(
            leading: Icon(Icons.link_outlined),
            title: Text('各種リンク集'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => Link()),
              );
            },
          ),
          ListTile(
            leading: Icon(Icons.call_outlined),
            title: Text('各種連絡先'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => Call()),
              );
            },
          ),

          Divider(),
          ListTile(
            leading: Icon(Icons.settings_outlined),
            title: Text('設定/その他'),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => Setting()),
              );
            },
          ),


        ],
      ),
    );
  }
}

Code to retrieve account information from Firestore (I want to use this to retrieve information)

FirebaseAuth.instance
  .authStateChanges()
  .listen((User? user) {
    if (user != null) {
      print(user.uid);
    }
  });

2

Answers


  1. To get access to user data in firebase you can use the:

    FirebaseAuth.instance.currentUser;

    this will return a User object and inside it, you can get name, email, uid ..etc

    Eg: FirebaseAuth.instance.currentUser.name;

    Login or Signup to reply.
  2. You need to change your NavBar widget to a Stateful widget as you are using a Stream to listen to changes in the AuthState.
    You can put your StreamListener in the initState method of your Stateful widget

    Here is a sample code:

    class NavBar extends StatefulWidget {
      const NavBar({Key? key}) : super(key: key);
      
      @override
      _NavBarState createState() => new _NavBarState();
    }
    
    class _NavBarState extends State<NavBar> {
    
      @override
      initState() {
        // you can also declare variables to store the user information from the stream
        FirebaseAuth.instance
          .authStateChanges()
          .listen((User? user) {
            if (user != null) {
              print(user.uid);
            }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: [
              GestureDetector(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => account()),
                  );
                },
                child:
              UserAccountsDrawerHeader(
                accountName: Text('開発者',style: TextStyle(color: Colors.white),),
                accountEmail: Text('[email protected]',style: TextStyle(color: Colors.white),),
                currentAccountPicture: CircleAvatar(
                  child: ClipOval(
                    child: Image.network(
                      'https://pbs.twimg.com/profile_images/1494938183448281089/xXIv3xmE_400x400.jpg',
                      width: 90,
                      height: 90,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                decoration: BoxDecoration(
                  color: Colors.lightGreen,
                  image: DecorationImage(
                    image: NetworkImage(
                      'https://pbs.twimg.com/profile_banners/1394312681209749510/1634787753/1500x500',
                    ),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              ),
              ListTile(
                leading: Icon(Icons.event_available_outlined),
                title: Text('行事予定'),
                onTap: () {
                 launch('https://www.ous.ac.jp/common/files//285/20220311164731084854.pdf');
                },
              ),
              ListTile(
                leading: Icon(Icons.public_outlined),
                title: Text('マイログ'),
                onTap: () {
                  launchUrl(Uri.https('mylog.pub.ous.ac.jp', '/uprx/up/pk/pky501/Pky50101.xhtml'),mode:LaunchMode.externalApplication );
    
                },
              ),
              ListTile(
                leading: Icon(Icons.book_outlined),
                title: Text('学生便覧'),
                onTap: () {
                  launch('https://edu.career-tasu.jp/p/digital_pamph/frame.aspx?id=7540000-3-30&FL=0');
                },
              ),
              Divider(),
    
              ListTile(
                leading: Icon(Icons.link_outlined),
                title: Text('各種リンク集'),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Link()),
                  );
                },
              ),
              ListTile(
                leading: Icon(Icons.call_outlined),
                title: Text('各種連絡先'),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Call()),
                  );
                },
              ),
    
              Divider(),
              ListTile(
                leading: Icon(Icons.settings_outlined),
                title: Text('設定/その他'),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Setting()),
                  );
                },
              ),
    
    
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search