Im trying to add a username in every pages of a flutter application i made. i mean in the sidedrawers of my page Im new to flutter so im facing very difficulty in this help me
I tried using shared preferences but i cant do it as the the variable says as it is a future string. i made it to save the username when a person login and it works when i tried to gain it back i cant
Future<String> getuname() async {
final prefs = await SharedPreferences.getInstance();
final name = "username";
var user = prefs.getString(name);
var username = '${user[0].toUpperCase()}${user.substring(1)}';
print('get $username');
return username;
}
and i added this to sidedrawer
ListTile(
leading: Icon(Icons.user),
title: Text(_get()),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyProfilePage()),
);
}),
String uname = '';
@override
void initState() {
getuname().then((value) {
setState(() {
uname = value;
});
});
_selectedDay = DateTime.now();
_events = {
_selectedDay.subtract(Duration(days: 30)): ['Event A0', 'Event B0', 'Event C0'],
_selectedDay.subtract(Duration(days: 27)): ['Event A1'],
_selectedDay.subtract(Duration(days: 20)): ['Event A2', 'Event B2', 'Event C2', 'Event D2'],
_selectedDay.subtract(Duration(days: 16)): ['Event A3', 'Event B3'],
_selectedDay.subtract(Duration(days: 10)): ['Event A4', 'Event B4', 'Event C4'],
_selectedDay.subtract(Duration(days: 4)): ['Event A5', 'Event B5', 'Event C5'],
_selectedDay.subtract(Duration(days: 2)): ['Event A6', 'Event B6'],
_selectedDay: [uname, 'Event B7', 'Event C7', 'Event D7'],
_selectedDay.add(Duration(days: 1)): ['Event A8v', 'Event B8', 'Event C8', 'Event D8'],
_selectedDay.add(Duration(days: 3)): Set.from(['Event A9', 'Event A9', 'Event B9']).toList(),
_selectedDay.add(Duration(days: 7)): ['Event A10', 'Event B10', 'Event C10'],
_selectedDay.add(Duration(days: 11)): ['Event A11', 'Event B11'],
_selectedDay.add(Duration(days: 17)): ['Event A12', 'Event B12', 'Event C12', 'Event D12'],
_selectedDay.add(Duration(days: 22)): ['Event A13', 'Event B13'],
_selectedDay.add(Duration(days: 26)): ['Event A14', 'Event B14', 'Event C14'],
};
_selectedEvents = _events[_selectedDay] ?? [];
_visibleEvents = _events;
_visibleHolidays = _holidays;
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 400),
);
_controller.forward();
super.initState();
}
I cant get the value of username, but it works in widget but not inside the initstate. help me