PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How to get shared prefernces in build context

 August 17, 2022     flutter, output     No comments   

Issue

i am using flutter to make a widget AppBar Drawer and i saved datas in SharedPReferences... this is my Drawer.dart: class AppDrawer extends StatefulWidget {

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

class _AppDrawerState extends State<AppDrawer> {
  SharedPreferences prefs;

  Future<void> logOut() async {
    final prefs = await SharedPreferences.getInstance();

    prefs.clear();
    Navigator.pushReplacementNamed(
      context,
      '/login',
    );
  }

  @override
  Widget build(BuildContext context) {
    final deviceSize = MediaQuery.of(context).size;
    return Drawer(
      child: ListView(
        children: <Widget>[
          DrawerHeader(
            child: Text(prefs.getString('first_name')),
            decoration: BoxDecoration(
              color: Colors.red,
            ),
          ),
          ListTile(
            leading: Text('LogOut'),
            onTap: logOut,
          ),
        ],
      ),
    );
  }
}

the way i am using it to logout is working to clear them but when i want to use it in the widget build it give me a null response... i am trying to prefs.getString('first_name') but its giving me null :( what i am doing wrong here??


Solution

You need to implement it like.

class _AppDrawerState extends State<AppDrawer> {
  
  String name = "";
  SharedPreferences prefs;

  @override
  void initState() {
    super.initState();
    getName();
  }

  getName() async {
    final prefs = await SharedPreferences.getInstance();
    setState((){
      name = prefs.getString('first_name');
    });
  }

  Future<void> logOut() async {
    prefs.clear();
    Navigator.pushReplacementNamed(
      context,
      '/login',
    );
  }

  @override
  Widget build(BuildContext context) {
    final deviceSize = MediaQuery.of(context).size;
    return Drawer(
      child: ListView(
        children: <Widget>[
          DrawerHeader(
            child: Text(this.name),
            decoration: BoxDecoration(
              color: Colors.red,
            ),
          ),
          ListTile(
            leading: Text('LogOut'),
            onTap: logOut,
          ),
        ],
      ),
    );
  }
}

await SharedPreferences.getInstance(); is an async call, you can't make an async call inside initState method or build widget method. So, that I created a separate function to get preferences and then updated the state variable name.



Answered By - Ehtisham Shahid Abbasi
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing