Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormArray insert a FormGroup to an specific index does not work properly #66

Closed
ngoc-quoc-huynh opened this issue Dec 26, 2020 · 5 comments

Comments

@ngoc-quoc-huynh
Copy link

ngoc-quoc-huynh commented Dec 26, 2020

Hey I'm trying to copy a FormGroup in a FormArray if I press a button. Adding a new FormGroup to the array like in the code below works. But if I make a copy by using the insert method with a given index it doesn't work properly. Editing the original FormGroup, will change the value of the copy as well and vice versa. I want to copy a FormGroup in a FormArray and edit only one of them and not both. In this example I copy the first item and put it to index 0.
Example:
[0] - A

  • Copy [0]
    [0] - A (new)
    [1] - A (old)
  • Edit [0]
    [0] - AB
    [1] - AB <-- Should not change to AB
 FormGroup testtForm({
    String name,
  }) =>
      fb.group(<String, dynamic>{
        'name': <dynamic>[
          name ?? '',
          Validators.required,
        ],
      });

class TestPage extends StatelessWidget {
  static FormArray<dynamic> array = fb.array([testForm()]);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ReactiveFormArray(
          formArray: array,
          builder: (
            BuildContext context,
            FormArray formArray,
            Widget child,
          ) {
            return ListView.separated(
                itemBuilder: (context, index) {
                  return ReactiveForm(
                    formGroup: formArray.controls[index] as FormGroup,
                    child: ReactiveTextField(
                      formControlName: 'name',
                    ),
                  );
                },
                separatorBuilder: (context, index) {
                  return SizedBox(
                    height: 10,
                  );
                },
                itemCount: formArray.controls.length);
          }),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            child: Icon(Icons.copy),
            onPressed: () {
              array.insert(
                0,
                  testForm(name: (array.controls[0] as FormGroup).control('name').value as String),
                
              );
            },
          ),
          FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              array.add(
                testForm(name: (array.controls[0] as FormGroup).control('name').value as String),
              );
            },
          ),
        ],
      ),
    );
  }
}

@joanpablo
Copy link
Owner

Hi @gucci-quoci,

Thanks for using Reactive Forms and for the issue. If I understand well, the code above actually execute well without problems. Could you please write and example about the code that actually doesn't work, so I can better understand the issue.

Thanks.

@ngoc-quoc-huynh
Copy link
Author

Hi @gucci-quoci,

Thanks for using Reactive Forms and for the issue. If I understand well, the code above actually execute well without problems. Could you please write and example about the code that actually doesn't work, so I can better understand the issue.

Thanks.

Hey I added an example. The code above works only for the add button. If I use the copy button it does not work properly.

@ngoc-quoc-huynh
Copy link
Author

Hey @joanpablo this is the code, which does not work properly.
Example of an input (Array index - ReactiveTextField value):

  • Start
    [0] - A
  • Copy [0]
    [0] - A (new)
    [1] - A (old)
  • Edit [0]
    [0] - AB
    [1] - AB <-- Should not change to AB
array.insert(0, testForm(name: (array.controls[0] as FormGroup).control('name').value as String));

@joanpablo
Copy link
Owner

joanpablo commented Jan 3, 2021

Hi @gucci-quoci,

Sorry for the late answer. The issue there is because of how Flutter handles elements in a list in the UI. If you put a key to all your ReactiveTextField the issue will be resolved.

final group = array.controls[i] as FormGroup;
ReactiveTextField(
   key: ObjectKey(group),
   formControlName: 'name',
),

@ngoc-quoc-huynh
Copy link
Author

Hi @joanpablo,
Thank you, I forgot about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants