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 = "";
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)),
),
),
),
);
}
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;
}
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.