I want to get user data from SharedPreferences in json format. whenever I want to show this data in screen it will show on screen but in console it displayed this error "The method '[]' was called on null. Receiver: null Tried calling: ". the error are throwing by this code ${userData['id']}
class _ProfileState extends State<Profile> { var userData; void initState(){ getUserData(); super.initState(); } void getUserData() async{ SharedPreferences localStorage = await SharedPreferences.getInstance(); var userJson = localStorage.getString('user'); var user = json.decode(userJson); setState(() { userData = user; }); } Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Center( child: Text("Profile"), ), actions: <Widget>[LoginPopup()], ), drawer: AppDrawer(), body:Padding( padding: EdgeInsets.all(10), child: ListView( children: [ Container( child: Center( child: Text('${userData['id']}'), ), ), ], ) ), ); } }