The getter 'DatabaseHelper' isn't defined for the class '_MyCustomFormState'

769 Views Asked by At
 int i = await DatabaseHelper.instance.insert(
                {
                  DatabaseHelper.colName: controller.text,
                },
              );
              print('New id inserted is $i');

DatabaseHelper is a singleton class - used for string data table via SQFlite.

Complete_Error: Error: The getter 'DatabaseHelper' isn't defined for the class '_MyCustomFormState'. - '_MyCustomFormState' is from 'package:textfieldvalidation/main.dart' ('lib/main.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'DatabaseHelper'. int i = await DatabaseHelper.instance.insert(

import 'package:textfieldvalidation/database_helper.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Form Validation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("Form Validation Demo"),
        ),
        body: MyCustomForm(),
      ),
    );
  }
}

//Create a Form Widget
class MyCustomForm extends StatefulWidget {
  @override
  _MyCustomFormState createState() => _MyCustomFormState();
}

//Create a corrsponding State class.
//This class holds data related to the form.
class _MyCustomFormState extends State<MyCustomForm> {
  //Create a global key that uniquely identifies the Form Widget
  //and allows validation of the form.
  //
  //Note:
  //This is a GlobalKey<FormState>,
  //not a GLobalKey<MyCustomFormState>
  final _formKey = GlobalKey<FormState>();
  final controller = TextEditingController();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //Build a Form widget using the _formKey created above.
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextFormField(
            controller: controller,
            validator: (value) {
              if (value.isEmpty) {
                return 'Please enter some text.';
              }
              return null;
            },
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16),
            child: ElevatedButton(
              onPressed: () async {
                if (_formKey.currentState.validate()) {
                  print(controller.text);
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: Text('Processing Data'),
                    ),
                  );
                  int i = await DatabaseHelper.instance.insert(
                    {
                      DatabaseHelper.colName: controller.text,
                    },
                  );
                  print('New id inserted is $i');
                }
              },
              child: Text("Submit"),
            ),
          ),
        ],
      ),
    );
  }
}
   
1

There are 1 best solutions below

0
On

Solved the issue by adding this line of code. final dbHelper = DatabaseHelper.instance; after initialising controller final controller = TextEditingController(); Then replace DatabaseHelper.instance.insert with this line of code dbHelper.insert