0

after I've retrieved data (an user) from an http request, I want to put this data into a singleton. Then, in another view, I want to show the name of this user in the sidebar, but it says "The getter 'user' was called on null." Here's my code:

var user = await getUser(_username, _password);

      if (user.loginError == false){
        model.user = user;

        Navigator.of(context).pushNamedAndRemoveUntil(
            '/home_view', ModalRoute.withName('/home_view'));
       }

And here's when I try to use the user.name:

Widget hamburgerMenu() {
  Model model;
  return new Drawer(
    child: ListView(
      children: <Widget>[
        new UserAccountsDrawerHeader(
          accountName: Text(model.user.name,
            ...
Little Monkey
  • 5,395
  • 14
  • 45
  • 83

1 Answers1

2

In hamburgerMenu() you are declaring the Model model; but you never asign any value to it. It will always be null. If it's a singleton you need to do something like this

Widget hamburgerMenu() {
  Model model = Model.instance;//You fetch the instance of the singleton
  return new Drawer(
    child: ListView(
      children: <Widget>[
        new UserAccountsDrawerHeader(
          accountName: Text(model.user.name,
            ...

Also, don't do this inside the build function. If it's a stateful widget do it in the initState() function

Sebastian
  • 3,666
  • 2
  • 19
  • 32