0

sup

I need some help with my app.

I have two custom AppBars, the first is used when the user is not logged. The second one is displayed after the login.

I just implemented a API call that returns the user data and updates the AppBar with the username.

How do I persist the AppBar with the username between Navigator calls? It's costly to call the API every time the context is updated.

Currently, each page of my App returns a Scaffold with AppBar and body attributes

Thank you.

  • See this also: https://stackoverflow.com/questions/49491860/flutter-how-to-correctly-use-an-inherited-widget – Blasanka Jan 19 '20 at 00:36
  • Welcome to Stack Overflow! T Please edit the question to show what you've tried, and show a specific roadblock you're running into with [mcve]. For more information, please see [ask]. – Erty Seidohl Jan 20 '20 at 04:07

1 Answers1

0

You can use Provider to consume the data:

Consumer<UserData>(
  builder: (context, userData, child) {
    return Scaffold(
      appBar: AppBar(title: Text(userData.username)),
    );
  }
)

And in some place (a parent Widget like the MaterialApp) you have to create the Provider for UserData:

Provider(
  create: (_) => UserData('the user name'),
  child: MaterialApp(...)
)

You can explore the Provider documentation to see wich type of provider fits better for your use case.