Home > Back-end >  Flutter / Dart / FutureBuilder - Using Logic method after builder
Flutter / Dart / FutureBuilder - Using Logic method after builder

Time:05-01

I'm trying to use method including logic, after Futurebuilder - builder. but I have to save my snapshot in builder and use it in my logic. I've saw this reference https://github.com/flutter/flutter/issues/16218#issuecomment-403995076

it says 'The logic should be added in the place that triggered the future (e.g. your initState), using the regular future logic (.then, await, or whatever you want to use).'

but I'm not get used to play with futurebuilder, initState, await or then.

I considered using addPostFrameCallback. but don't know where to use it & what should be inside.

can anyone tell me more details or recommand reference about it??

thanks a lot.

in my futurebuilder

  recipeList() {
return FutureBuilder<Recipes>(
    future: recipeController.recipe,
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        recipeController.snapshots = snapshot; // ->> I need it to excute recipeFinder
        recipeController.recipeFinder(); // --> Causing Error
        return Container(...

in my GetxController

 late AsyncSnapshot<Recipes> snapshots = AsyncSnapshot.nothing();
  ...
  recipeFinder() async {
    avaliableRecipe.clear(); 
    for (int i = 0; i < 1358; i  ) {
      int count = 0;
      results =
          snapshots.data!.COOKRCP02.row.elementAt(i).RCPPARTSDTLS.split(',');
      for (int j = 0; j < results.length; j  ) {

error code

E/flutter (24674): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() or markNeedsBuild() called during build.
E/flutter (24674): This Obx widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
E/flutter (24674): The widget on which setState() or markNeedsBuild() was called was:
E/flutter (24674):   Obx
E/flutter (24674): The widget which was currently being built when the offending call was made was:
E/flutter (24674):   FutureBuilder<Recipes>

CodePudding user response:

I would look into Provider. You could set up a Provider that initially loads this data. You can handle all the logic and Futures within this Class, and then use Provider.of(... to access it in the Widget, where you get the Future with the already applied logic. You can also have a bool inside the Provider that is false until the data has loaded, and use this bool in the widget to either return the Widget or a Loading Indicator.

  • Related