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

Multiple widgets using the same GlobalKey #193

Closed
SGarno opened this issue Dec 8, 2019 · 4 comments
Closed

Multiple widgets using the same GlobalKey #193

SGarno opened this issue Dec 8, 2019 · 4 comments

Comments

@SGarno
Copy link

SGarno commented Dec 8, 2019

I am trying to wrap the form builder in a routine which generates the form dynamically from a set of form field descriptors, but am getting the above error.

Here is the code:

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:testflutter/model/FormItem.dart';

import '../types.dart';

class FormGenerator extends StatelessWidget {

  final Key key;
  final List<FormItem> formItems;

  FormGenerator({this.key, this.formItems});

  Widget _buildTextField( FormItem item ) {
    return FormBuilderTextField(
      attribute: item.name,
      decoration: InputDecoration(labelText: item.title),
      validators: [
      ],
    );   
  }

  Widget _buildRadioField( FormItem item ) {
    return Text("...coming soon ...");      
  }

  Widget _buildColorPicker( FormItem item ) {
    return Text("...coming soon ...");      
  }

  Widget _buildSwitch( FormItem item ) {
    return Text("...coming soon ...");      
  }

  Widget _buildCheckbox( FormItem item ) {
    return Text("...coming soon ...");      
  }

  List<Widget> _buildForm( ) {

    List<Widget> widgetList = new List<Widget>();

    for (var count = 0; count < this.formItems.length; count++) {
      FormItem field = this.formItems[count];

      switch( field.type ) {
        case FieldType.input:
        case FieldType.password:
        case FieldType.email:
        case FieldType.numeric:
        case FieldType.textarea: 
          widgetList.add( _buildTextField(field) );
          break;
        case FieldType.radiobutton: 
          widgetList.add( _buildRadioField(field) );
          break;
        case FieldType.color:
          widgetList.add( _buildColorPicker(field) );
          break;
        case FieldType.toggle:
          widgetList.add( _buildSwitch(field) );
          break;
        case FieldType.checkbox:
          widgetList.add( _buildCheckbox(field) );
          break;
      }

    }
    return widgetList;
  }


  @override
  Widget build( BuildContext context ) {

    return FormBuilder(
      key: key,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: _buildForm(),
      )
    );
  }
}

The key is being generated in the parent class outside of the build function.

What am I missing/doing wrong?

@SGarno
Copy link
Author

SGarno commented Dec 9, 2019

I found a fix, but am not sure I understand why. By renaming the field from key to fbKey, it all worked. I am only passing that value to FormBuilder, so not sure why that would make a difference?

@danvick
Copy link
Collaborator

danvick commented May 7, 2020

FormBuilder works well with a GlobalKey, this allows you to have a handle on its state. Also, note that your field attribute names (i.e. item.name) should not be the same. If at all there are duplicates that error will be thrown.

@danvick danvick closed this as completed May 7, 2020
@akreienbring
Copy link

Hi, I also face this error with the FormBuilder used in two different stateful widgets.

Declared like in the example:

class MyState1 extends State<MyState1Route>{
final GlobalKey<FormBuilderState> _fbKeyForm1 = GlobalKey<FormBuilderState>();
class MyState2 extends State<MyState2Route>{
final GlobalKey<FormBuilderState> _fbKeyForm2 = GlobalKey<FormBuilderState>();

This immediately gives me an error:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Multiple widgets used the same GlobalKey.

The proposals from this Stackoverview question did not help.
https://stackoverflow.com/questions/49862572/multiple-widgets-used-the-same-globalkey

I don't think that this is related to flutter_form_builder. Looks like GlobalKey is considered to be the same by flutter.

Anyway, I wanted to let you know and maybe you found a solution for this or know what I'm doing wrong..

@akreienbring
Copy link

Ok, forget about it. I accidentally was INDEED using the key twice!

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

3 participants