0

I have a fixed-length List that is used in the "build" function. I want to replace one element in that List with another. Both elements in the List are Containers. I replace the element in the List and do a setState(). The build function uses the List in the body of the Scaffold. I get no error, however when the rebuild is done, nothing has changed.

To my knowledge this is not breaking any rules and as far as I know should cause no problems.

While I could code around the issue, I think that with something as fundamental as this, I need to find the reason for it.

Any ideas as to why this is happening?

Code added below:

Below is the code that DOES NOT work

    if (_lwDisplay[iNdxDisplay] == null) /*INITIAL DISPLAY */ {
    _lwDisplay[iNdxDisplay] = wContainer;
    } else {
      setState(() {
        _tfDataHasChanged = true;
        _lwDisplay[iNdxDisplay] = wContainer;
      });
    }

Below is the code that DOES work

  if (_lwDisplay[iNdxDisplay] == null) /*INITIAL DISPLAY */ {
      _lwDisplay[iNdxDisplay] = wContainer;
    } else {
       _tfDataHasChanged = _fnHasDataChanged() /* FOR FAB */;
      List<Widget> lwDisplay2 = List(_lwDisplay.length);
      for (int iNdx = 0; iNdx < lwDisplay2.length; iNdx++) {
        if (iNdx != iNdxDisplay) {
          lwDisplay2[iNdx] = _lwDisplay[iNdx];
        }
      }
      lwDisplay2[iNdxDisplay] = wContainer;
      _lwDisplay = lwDisplay2;
      setState(() {});
    }
Brian Oh
  • 9,604
  • 12
  • 49
  • 68
  • Can you share some code with us so we can see what you are actually doing? – tomerpacific Feb 10 '20 at 08:00
  • Have added code to my question. – Brian Oh Feb 10 '20 at 08:06
  • posting source code that all defined in one single main.dart would be great for any one to help you easily. Also add expected and actual output. – Darish Feb 10 '20 at 08:36
  • @BrianOh - where have you created your list of widgets in your program? – tomerpacific Feb 10 '20 at 08:43
  • @tomerpacific - I have created the widgets after the first build in a function called by onBuildComplete, IE after the first build. The program in question is fairly large, because I need to replicate a problem I am experiencing in another program that is part of an app, and to do that, I need to emulate that program as closely as possible. – Brian Oh Feb 10 '20 at 08:52
  • @Darish - I would do that Darish, but, the program is a test program to emulate a program that is part of an app. To create a test environment, I have created this as closely as possible to the original program. I will try what you say, and if I can replicate it in something more manageable, I will do so. I'll try it with something more simple and see if I get the same result. The test program and related components is fairly large. – Brian Oh Feb 10 '20 at 08:58
  • @BrianOh kindly prepare that sample code, otherwise it is almost impossible to guess what went wrong. – Darish Feb 10 '20 at 09:11
  • if it is a large main.dart file, you may post it as a gist on github.com and share the link. – Darish Feb 10 '20 at 09:12
  • @Darish - OK, I'll try a smaller program first, but I suspect it may be particular circumstances that cause it to happen. – Brian Oh Feb 10 '20 at 09:55

1 Answers1

1

Without seeing the entirety of code, and according to this SO question,

Move the instantiation of the list of widgets to the build method of your main widget.

I.E.

class MainWidgetState extends State<MainWidget> {
      List<YOUR_WIDGET> _lwDisplay = new List();

     Widget build(BuildContext context) {
          //Create your list
     }
}

Also, add a key to each item in your widget (if you haven't done so already).

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
  • 1
    Thanks, I will check that out asap, so I'll get back to you here. – Brian Oh Feb 10 '20 at 12:28
  • I have spent quite a lot of time attempting to solve this, and thanks for your help. I did create another smaller test program to replicate the problem, but it worked without any problems. I did what you suggested regarding creation in the build, and using a key, but the program still malfunctioned. In the end, what I have just done, is in the build I create another List as a local variable, and copy the global List over element to it. This local List is then used by the build and then discarded. That works. – Brian Oh Feb 11 '20 at 07:45
  • @BrianOh - glad to hear things worked out. If my answer helped in any way, please mark it (or upvote). – tomerpacific Feb 11 '20 at 07:51
  • OK, I up-voted because it shows me I need to learn about Widget Keys. – Brian Oh Feb 11 '20 at 09:05