skip to Main Content

I’m developing a flutter app, in my app I have a userData model to store data about a user. I then have a profile page where it is diplayed almost all of that data. I use Firebase to store those userData objects.

As I have it right now, I have a futureBuilder() for every widgets that needs data about a user, i.e I have a text() widget that displays the user name, inside a futureBuilder which the future is a method that retrieves the name field from firebase doc.

I was wondering if it was better to have the whole page wrapped in a futureBuiler which the future in a method that retrieves the userData object instead of a field at a time, and then build all widgets inside that futureBuilder.

body: Stack(   
        children: [
          _profileBanner(), //futureBuilder
          _whiteBackgroundContainer(),
          _profileImage(), //futureBuilder
          _userName(), //futureBuilder
          _backArrow(),
          _profilePageButtons(),
          _reputationBuble(), //futureBuilder
          _friendsAndGamesCounter(),//futureBuilder
          _personnaInformationRow(), //futureBuilder
          _divider(),
          _biographyTile(), //futureBuilder
          _sportsList(), //futureBuilder
        ],
      )

2

Answers


  1. Well yes, when you are working with APIs or remote databases in general you need to use the approach that uses the least amount of HTTP calls, in this instance if you can fetch the user data all at once do that and have 1 big widget "the profile screen" in a future builder and use the response to populate your UI

    PS: avoid using functions for UI elements instead use stateless classes

    Login or Signup to reply.
  2. I agree with Pierre.

    This is a great example where Riverpod can add value where you save writing a lot of wiring and mgmt code. Ultimately it is up to you, and your app. But if you find yourself reading the same pieces of data in many places, having to keep all these places up to date when that data changes, consider a state management solution.

    Unfortunately (or fortunately?) Flutter doesn’t have a prescribed state management solution, so it is more of a pick your own adventure.

    But if you increasingly run into these state issues, find yourself writing and maintaining glue code that has to get the same state, notify many places when that state changes, it’s a sign that a library can help.

    You can use Riverpod, Flutter Hooks, use a service locator like GetX and keep this data in a single place, whatever works for your solution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search