簡體   English   中英

未為類“_MyCustomFormState”定義 getter“DatabaseHelper”

[英]The getter 'DatabaseHelper' isn't defined for the class '_MyCustomFormState'

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

DatabaseHelper 是一個單例類 - 通過 SQFlite 用於字符串數據表。

Complete_Error:錯誤:未為類“_MyCustomFormState”定義 getter“DatabaseHelper”。 - '_MyCustomFormState' 來自 'package:textfieldvalidation/main.dart' ('lib/main.dart')。 嘗試將名稱更正為現有 getter 的名稱,或定義名為“DatabaseHelper”的 getter 或字段。 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"),
            ),
          ),
        ],
      ),
    );
  }
}
   

通過添加這行代碼解決了這個問題。 final dbHelper = DatabaseHelper.instance; 初始化控制器后final controller = TextEditingController(); 然后用這行代碼dbHelper.insert替換DatabaseHelper.instance.insert

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM