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

State dos not contain all fields #1360

Closed
2 of 7 tasks
SergeBerwert opened this issue Jan 30, 2024 · 2 comments
Closed
2 of 7 tasks

State dos not contain all fields #1360

SergeBerwert opened this issue Jan 30, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@SergeBerwert
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.2.0

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.8, on Microsoft Windows [Version 10.0.22621.3085], locale en-CH)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.8.5)
[!] Android Studio (version 2022.3)
    X Unable to determine bundled Java version.
[√] Android Studio (version 2023.1)
[√] VS Code (version 1.85.2)
[√] Connected device (3 available)
[√] Network resources

Minimal code example

Code sample
class SchemaEditBuilder extends StatelessWidget {
  SchemaEditBuilder({required this.binder, required this.onSave, super.key});

  final FormBinder binder;
  final Function(List<UpdateOp> updates) onSave;

  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return FormBuilder(
      key: _formKey,
      child: ListView(
        children: [
          //if (state is UserActions) ContentActions(state: state as UserActions),
          Row(children: [
            ActionButton(
                icon: Icons.save,
                name: "Save",
                onPressed: () {
                  _formKey.currentState?.save();
                  var updates = _formKey.currentState!.updateObject();
                  onSave(updates);
                })
          ]),
          ..._buildSchema(binder)
        ],
      ),
    );
  }

  Iterable<Widget> _buildSchema(FormBinder binder) sync* {
    for (var p in binder.schema.properties.entries) {
      switch (p.value.type) {
        case SchemaType.Dictionary:
          yield* _buildSchema(binder.getChild(p.value, p.key));
          break;

        case SchemaType.Object_:
          var child = binder.getChild(p.value, p.key);
          yield SectionTitleWidget(child.getTitle());
          yield* _buildSchema(child);
          break;

        default:
          var b = binder.getBinder(p.value, p.key);
          yield SectionTitleWidget(b.getTitle());
          yield _buildWidget(b);
      }
    }
  }

  Widget _buildWidget(FormBinder binder) {
    switch (binder.schema.type) {
      case SchemaType.String:
        return FormBuilderTextField(
            key: ValueKey(binder.path),
            name: binder.key,
            initialValue: binder.getStringValue());

      case SchemaType.Boolean:
        return Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            SizedBox(
              width: 200,
              child: FormBuilderSwitch(
                  key: ValueKey(binder.path),
                  name: binder.key,
                  title: Text(binder.getTitle()),
                  initialValue: binder.getBoolValue()),
            ),
          ],
        );

      default:
        return Text("Type: ${binder.schema.type} not supported");
    }
  }
}

Current Behavior

I build a form using Formbuilder the form is generated by a JSON schema-like structure. all expected fields show up in the GUI but the stat fields are missing after saving. Fields are only 7 but values are 11. this is a problem because I use fields to save the values on the server. I check each value if it's dirty

2024-01-30_08-26-39

Expected Behavior

expect that all fields i add are also in the fields collection.

Steps To Reproduce

I don't have a quick way to reproduce it for now.

Aditional information

No response

@SergeBerwert SergeBerwert added the bug Something isn't working label Jan 30, 2024
@JanneckLange
Copy link

JanneckLange commented Mar 6, 2024

I had same Problem with FormBuilder and ListView. I switched to SingleChildScrollView
This Problem also affects validation. Fields missing in the state will not validate and appear to be correct.

FormBuilder(
    key: _formKey,
        child: SingleChildScrollView(
            child: Column(
                children: [
                      // FormFields
                ]
           )
       )

@deandreamatias
Copy link
Collaborator

I think that this is related with this comment #1426 (comment)

I will close this issue. If want to reopen, please add a minimal code example to can execute:

  • Without external packages and dependencies
  • Without refereces of imports
  • With only Flutter Material or Cupertino widgets and FormBuilder widgets
  • Starting from main Dart method

Example:

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:form_builder_validators/form_builder_validators.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter FormBuilder Example',
      debugShowCheckedModeBanner: false,
      localizationsDelegates: const [
        FormBuilderLocalizations.delegate,
        ...GlobalMaterialLocalizations.delegates,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: FormBuilderLocalizations.supportedLocales,
      home: const _ExamplePage(),
    );
  }
}

class _ExamplePage extends StatefulWidget {
  const _ExamplePage();

  @override
  State<_ExamplePage> createState() => _ExamplePageState();
}

class _ExamplePageState extends State<_ExamplePage> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FormBuilder(
        key: _formKey,
        child: Column(
          children: [
            FormBuilderTextField(
              name: 'full_name',
              decoration: const InputDecoration(labelText: 'Full Name'),
              validator: FormBuilderValidators.compose([
                FormBuilderValidators.required(),
              ]),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () {
                _formKey.currentState?.saveAndValidate();
                debugPrint(_formKey.currentState?.value.toString());
              },
              child: const Text('Print'),
            )
          ],
        ),
      ),
    );
  }
}

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants