Open SebyOnGitHub opened 3 days ago
Hello!
I see what you’re pointing out, and this might actually be the expected behavior when using Page State variables. Page State variables are essentially variables declared in the model of the page. For example:
dynamic jsonState1;
dynamic jsonState2;
So, when you run code like this:
if (input is Map<String, dynamic>) {
// Remove entries where the key is 'name'
input.remove('name');
}
It modifies the variable directly as well.
Hi, thanks for having a look. I also created another page that does the same thing, but uses a string instead of JSON. Here you can see that the second string state is only edited and the first string state remains unchanged:
You can try it yourself, the second page is in the same project. As we can see, this scenario behaves unpredictably. The JSON on the first state should remain unchanged. The function only returns a different value and should never change a page or component state.
Can we access your project?
Current Behavior
When a state (JSON 1) is set to another JSON state (JSON 2), the first JSON state (JSON 1) is also changed. While we are setting JSON 2, we are changing the JSON a little bit with a function. But the changes from that function also change JSON 1, even if we are only using a set state for JSON 2.
Expected Behavior
The state of JSON 1 should not be changed, only JSON 2 should be changed because we are only setting the state of JSON 2.
Steps to Reproduce
New Project / New Page
Create two JSON page states. You can name them jsonState1 and jsonState2.
Create logic to set jsonState1 (I did this by using a TextField and a button that inserts the string into jsonState1).
Create another button that inserts the value of jsonState1 into jsonState2. It is important that we use a function in between that changes something. This example function removes the key "name". Here we can see that we only change the state jsonState2. Later we will see that not only this state is changed.
Reproducible from Blank
Bug Report Code (Required)
ITFTls/l7c92rsMA+KrQYexKpDgWGUMdXOczitVEew4bCIj4BLMQOuD7fk1uUuqzaH1EBFCWrHgCptbHj/DPEe1dBy2ZRoBJ/6tIWBPPbEyQM7aNCpDpZX8lDcBWIH6y4aSRhCQkB7dZdGAa3zuQFefITT+Cf9qOYwx5e6fDbOY=
Visual documentation
Here is what happens when we use this example: After compiling it looks like this, both States are empty.
Then insert a sample JSON like this:
Then press the first button and notice that jsonState1 has changed:
So far, everything is working as expected. Now when we press the second button and try to insert jsonState1 into jsonState2. This also works, it first removes the key "name" and then sets it into jsonState2. But jsonState1 is also changed. jsonState1 should stay unchanged, this is wrong behaviour. The jsonState1 should remain unchanged and keep the key name.
Environment
Additional Information
This has been tested when compiled for the web, not on different platforms. However, the functions used here can easily be viewed in the Function Editor. Both functions are kept extremely simple to illustrate the problem/bug. This whole example project is just an extremely simplified example to demonstrate this misbehaviour. If needed, we can show this problem on a larger scale in a real project.