flutterdartflutter-form-builder

Error: The getter 'FormBuilderValidators' isn't defined


I am getting the following error from flutter:

lib/screens/create_account_screen.dart:30:30: Error: The getter 'FormBuilderValidators' isn't defined for the class '_CreateAccountScreenState'.
 - '_CreateAccountScreenState' is from 'package:myapp/screens/create_account_screen.dart' ('lib/screens/create_account_screen.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'FormBuilderValidators'.
                  validator: FormBuilderValidators.compose([

I have included the validator in my pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  flutter_isolate: ^2.0.4
  flutter_form_builder: ^9.1.0
  #flutter_form_builder: ^7.0.0
  image_picker: ^0.8.4

I've done a "flutter pub get" which was successful. I can see the library is in the expected place:

.pub-cache/hosted/pub.dev/flutter_form_builder-9.1.0 

I've done:

flutter clean
flutter pub get
flutter build apk

This fails from the command line and from my IDE. I'm very puzzed as to why I'm getting this error. I've opened and closed my IDE (sometimes it doesn't pick up dependencies unless I do this).

Any ideas where to go from here? I want to use FormBuilderValidators in my project if I can get it to compile.

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

class CreateAccountScreen extends StatefulWidget {
  @override
  _CreateAccountScreenState createState() => _CreateAccountScreenState();
}

class _CreateAccountScreenState extends State<CreateAccountScreen> {
  final GlobalKey<FormBuilderState> _formKey = GlobalKey<FormBuilderState>();
  bool _isPasswordVisible = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Create Account')),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: FormBuilder(
            key: _formKey,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // ... other TextFields ...

                FormBuilderTextField(
                  name: 'email',
                  decoration: InputDecoration(labelText: 'Email'),
                  validator: FormBuilderValidators.compose([
                    FormBuilderValidators.required(),
                    FormBuilderValidators.email(),
                  ]),
                ),
                SizedBox(height: 16),

                FormBuilderTextField(
                  name: 'phone',
                  decoration: InputDecoration(labelText: 'Phone Number'),
                  validator: FormBuilderValidators.compose([
                    FormBuilderValidators.required(context),
                    FormBuilderValidators.phone(context),
                  ]),
                ),
                SizedBox(height: 16),

                FormBuilderTextField(
                  name: 'password',
                  decoration: InputDecoration(
                    labelText: 'Password',
                    suffixIcon: IconButton(
                      icon: Icon(_isPasswordVisible
                          ? Icons.visibility
                          : Icons.visibility_off),
                      onPressed: _togglePasswordVisibility,
                    ),
                  ),
                  obscureText: !_isPasswordVisible,
                  validator: FormBuilderValidators.compose([
                    FormBuilderValidators.required(context),
                    FormBuilderValidators.minLength(context, 6),
                  ]),
                ),
                // ... rest of the widgets ...
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _togglePasswordVisibility() {
    setState(() {
      _isPasswordVisible = !_isPasswordVisible;
    });
  }
}

Solution

  • You also used FormFieldValidator class. It's coming from another package. If you check flutter_form_builder's example tab. You'll see and guess everything.

    So, You should add another package to your dependency.

    run flutter pub add form_builder_validators