int i = await DatabaseHelper.instance.insert(
{
DatabaseHelper.colName: controller.text,
},
);
print('New id inserted is $i');
DatabaseHelper是一个单例类-通过SQFlite用于字符串数据表。
Complete_Error: 错误:未为类“ _MyCustomFormState”定义吸气剂“ DatabaseHelper”。 -“ _MyCustomFormState”来自“ package:textfieldvalidation / main.dart”(“ lib / main.dart”)。 尝试将名称更正为现有吸气剂的名称,或定义一个名为“ DatabaseHelper”的吸气剂或字段。 int i =等待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"),
),
),
],
),
);
}
}
答案 0 :(得分:0)
通过添加以下代码行解决了该问题。
初始化控制器后的final dbHelper = DatabaseHelper.instance;
final controller = TextEditingController();
然后用此行代码替换DatabaseHelper.instance.insert
dbHelper.insert