-1

I would like to ask for some help. I have a login form connected with firebase. I have 3 more pages which display the current user email. However, I decided to implement a bottom nav menu bar and I am stuck. As you can see from the picture, I cant pass the current user email in the bottom nav bar. Currently, it is hard coded. Is it possible for someone to give me any advice?

//bottom nav menu

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:test_honours/db/auth_controller.dart';
import 'package:test_honours/screens/profile_page.dart';
import 'package:test_honours/screens/welcome_page.dart';
import 'package:test_honours/screens/bmi_input_page.dart';
import 'package:custom_navigation_bar/custom_navigation_bar.dart';

import 'package:test_honours/screens/welcome_page.dart';
import 'package:test_honours/screens/bmi_input_page.dart';
import 'package:test_honours/screens/profile_page.dart';

class MyNavigationBar extends StatefulWidget {
  String? email;
  MyNavigationBar({Key? key, required this.email}) : super(key: key);

  @override
  _MyNavigationBarState createState() => _MyNavigationBarState();
}

class _MyNavigationBarState extends State<MyNavigationBar> {
  int _selectedIndex = 0;

  final currentUser = FirebaseAuth.instance;

  final List<Widget> _widgetOptions = <Widget>[
    WelcomePage(email: ""),
    BMICalculator(),
    BMICalculator(),
    ProfilePage(email: "email")
  ];

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: _widgetOptions[_selectedIndex],
      bottomNavigationBar: CustomNavigationBar(
        isFloating: true,
        elevation: 2,
        iconSize: 30,
        selectedColor: Colors.blueAccent,
        items: [
          CustomNavigationBarItem(
              icon: Icon(Icons.home_outlined)), //welcome page
          CustomNavigationBarItem(
              icon: Icon(Icons.location_on_outlined)), //gps location page
          CustomNavigationBarItem(
              icon: Icon(Icons
                  .medical_services_outlined)), //bmi calculator or future appointments
          CustomNavigationBarItem(icon: Icon(Icons.settings))
        ],
        onTap: (i) {
          setState(() {
            _selectedIndex = i;
          });
        },
        currentIndex: _selectedIndex,
      ),
    ));
  }
}

//welcome page:

class WelcomePage extends StatelessWidget {
  String email;
  WelcomePage({Key? key, required this.email}) : super(key: key);

.............
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Don't paste code as images. Remove the image and add the relevant code to the question. Make sure to add the definition of `WelcomePage` as well. – Robert Sandberg Feb 11 '23 at 13:04

2 Answers2

0

You can get email like

FirebaseAuth.instance.currentUser?.email
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • I tried. The argument type 'String?' can't be assigned to the parameter type 'String'. (Documentation) this is the error i am getting. i supposed i am not doing sth correctly that's why I posted the question. WelcomePage(email: FirebaseAuth.instance.currentUser?.email.toString()), – flutter_dev Feb 11 '23 at 13:38
  • you can add default value on null case as Rovert Sandberg showed with `?? "default"` also you can do a null check 1st, then use null-assert like `email!` – Md. Yeasin Sheikh Feb 11 '23 at 13:39
0

You could do this:

WelcomePage(email: FirebaseAuth.instance.currentUser?.email ?? 'SomeDefaultValue'),

Smarter:

late final String userEmail;
 
@override
void initState() {
  super.initState();
  userEmail = FirebaseAuth.instance.currentUser?.email ?? 'SomeDefaultValue';
}

....

WelcomePage(email: userEmail),
Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30
  • Thank you. Do you think the way I am declaring the user is correct? final currentUser = FirebaseAuth.instance; when i tried your advice i got this error: The instance member 'currentUser' can't be accessed in an initializer. (Documentation) Try replacing the reference to the instance member with a different expression – flutter_dev Feb 11 '23 at 13:43
  • Updated. Try again – Robert Sandberg Feb 11 '23 at 13:49
  • But smarter would be to initialize the current user in the initState method. Otherwise you'll fetch the instance on every build (up to 60 times per second) – Robert Sandberg Feb 11 '23 at 13:50