Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a function that will retrieve username from database and that username value will be stored in global string variable, everything works, and my goal is to display that username variable on the AppBar text but I keeps getting "Null is not a subtype type of string" error.

This is the main class

String _custUsername = "";

// User will be redirect to this page after logged-in
class _cakeHomeWidgetState extends State<cakeWidget> { 

  BuildContext? dialogContext;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: setupMainTheme,
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(58),
        child: AppBar(
          centerTitle: false,
          title: Column(
            children: <Widget>[
             Text(setupTime(),
                style: 
                  TextStyle(
                    color: Color.fromARGB(255, 190, 190, 190),
                    fontWeight: FontWeight.w700,
                    fontSize: 20
                    )
                ),
            ], 
          ),
          automaticallyImplyLeading: false,
          backgroundColor: setupMainTheme,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(
              bottom: Radius.circular(12)),
            ),
        ),
      ),
   );
  }


// User will see this page first 
class cakeLoginPageMain extends State<cakeLogin> {

  String? _CustEmailInit; 
  BuildContext? loginContext;

  Future<void> callData() async {

    var custUsernameGet = await UsernameGetter().Connection(_CustEmailInit!);
    var nameValuesGet = await NameGetter().Connection(custUsernameGet);

    setState(() {
      _custUsername = custUsernameGet;
    });

    print("USERNAME: " + _custUsername);

   await Navigator.push(context, 
                          MaterialPageRoute(
                            builder: (context) => const main.cakeWidget()
                            )
                          );

  }
  @override
  void initState() {
  }

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(
        elevation: 0,
        title: Text("Sign In",
          style: TextStyle(
            fontSize: 22,
            color: setupFontCol,
            fontWeight: FontWeight.bold
          )),
        backgroundColor: setupMainTheme,
        centerTitle: false,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        )
      ),

      backgroundColor: setupMainTheme,
      body: Padding(
        padding: EdgeInsets.all(28),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

            TextFormField(
              controller: _emailController,
              style: TextStyle(color: Color.fromARGB(255, 214, 213, 213)),
              decoration: InputDecoration(
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                filled: true,
                hintStyle: TextStyle(color: Color.fromARGB(255, 197, 197, 197)),
                hintText: "Enter your email",
                fillColor: setupMainTheme,
              ),
            ),

          SizedBox(
            height: 45,
            width: 500,
            child: ElevatedButton(
              style: 
                ElevatedButton.styleFrom(
                  primary: setupFontCol,
                  onPrimary: Colors.white,
                  shape: const StadiumBorder(),
                ),

              var _CustEmail = _emailController.text;

              onPressed: () {
                   _CustEmailInit = emailController.Text
                   callData();
           }
            child: Text("Sign In",
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.normal,
                  fontSize: 16,
                )) 
          ),
      ),      
    );
  }
}

String setupTime() {
  if(_custUsername != null) {
    var hour = DateTime.now().hour;
    if (hour < 12) {
      return '☀️   Good Morning ' + _custUsername;
    }
    if (hour < 17) {
      return '☀️   Good Afternoon ' + _custUsername;
    }
  } 
  return '🌙   Good Evening ' + _custUsername; // Debugger throws "Null is not a subtype type of string" error here
}


However when I prints the value of _custUsername in callData function it returns the value that I was expecting for example "Daniel" but it throws null error error inside the setupTime function.

What I have tried:

Created local variable inside setupTime() function then assign that local variable with _custUsername value and replace the _custUsername variable in that function with that locally assigned variable.
Posted
Updated 8-Mar-23 4:53am

1 solution

This is the same issue as your previous question at Type 'null' is not a subtype of type 'string' while trying to retrieve data from list into listview[^].

You need to look at the code at ...
Dart
var custUsernameGet = await UsernameGetter().Connection(_CustEmailInit!);
var nameValuesGet = await NameGetter().Connection(custUsernameGet);

setState(() {
  _custUsername = custUsernameGet;
});

... to see why it is returning null rather than the value you expect.


[edit]
See also Asynchronous programming: futures, async, await | Dart[^]
[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900