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

The method 'contains' was called on null in FormBuilderCheckboxList #68

Closed
shariful2011 opened this issue May 25, 2019 · 2 comments
Closed

Comments

@shariful2011
Copy link

I am getting the following exception with FormBuilderCheckboxList:
I/flutter ( 3712): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3712): The following NoSuchMethodError was thrown building
I/flutter ( 3712): FormField-[LabeledGlobalKey<FormFieldState>#9b66a](dirty, state:
I/flutter ( 3712): FormFieldState#3cadd):
I/flutter ( 3712): The method 'contains' was called on null.
I/flutter ( 3712): Receiver: null
I/flutter ( 3712): Tried calling: contains(0)
I/flutter ( 3712):
I/flutter ( 3712): When the exception was thrown, this was the stack:
I/flutter ( 3712): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
I/flutter ( 3712): #1 _FormBuilderCheckboxListState._checkbox
package:flutter_form_builder/…/fields/form_builder_checkbox_list.dart:55
I/flutter ( 3712): #2 _FormBuilderCheckboxListState._leading
package:flutter_form_builder/…/fields/form_builder_checkbox_list.dart:71
I/flutter ( 3712): #3 _FormBuilderCheckboxListState.build.
package:flutter_form_builder/…/fields/form_builder_checkbox_list.dart:108
I/flutter ( 3712): #4 FormFieldState.build (package:flutter/src/widgets/form.dart)

What's the way to work around?

Following is my code:
class

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

class MyCheckbox extends StatelessWidget {
final String attribute;
final List options;
final String title;
final bool mandatory;
final ValueChanged onChanged;
const MyCheckbox(
{Key key,
this.attribute,
this.options,
this.mandatory,
this.title,
this.onChanged})
: super(key: key);

String _labelText() {
return mandatory
? title.substring(0, 1).toUpperCase() + title.substring(1) + '*'
: title.substring(0, 1).toUpperCase() + title.substring(1);
}

@OverRide
Widget build(BuildContext context) {
return FormBuilderCheckboxList(
attribute: attribute,
options: options,
initialValue: null,
decoration: InputDecoration(
labelText: _labelText(),
labelStyle: TextStyle(
color: Color(0xFF4574EC),
),
border: InputBorder.none,
),
leadingInput: true,
validators: mandatory
? [
FormBuilderValidators.required(
errorText: 'Please select $title',
)
]
: [],
onChanged: onChanged,
);
}
}

calling it

MyCheckbox(
attribute: 'treatment',
mandatory: false,
title: 'Treatment received',
options: [
FormBuilderFieldOption(value: 0, label: 'IV Fluids'),
FormBuilderFieldOption(value: 1, label: 'Antibiotics'),
FormBuilderFieldOption(value: 2, label: 'ORS'),
],
onChanged: (dynamic val) {
print('treatmentreceived=$val');
setState(() {
_treatment = val;
});
},
),

@danvick
Copy link
Collaborator

danvick commented May 25, 2019

Change initialValue: null, to initialValue: [],

@danvick danvick closed this as completed May 25, 2019
@danvick
Copy link
Collaborator

danvick commented May 25, 2019

Or alternatively don't include initialValue at all because its not needed.

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