flutter - 缺少(state.build)的具体实现(插入到 sqflite)
问题描述
我正在尝试对 sqflite 数据进行文本字段输入,但我收到此错误消息:
Missing concrete implementation of 'State.build'. Try implementing the missing method, or make the class abstract.
任何人都可以帮助我吗?
正确的完整示例StateFulWidget
:
import 'package:flutter/material.dart';
import 'dart:async';
import '../sql.dart';
class AddItem extends StatefulWidget {
const AddItem({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<AddItem> {
// All journals
List<Map<String, dynamic>> _journals = [];
bool _isLoading = true;
// This function is used to fetch all data from the database
void _refreshJournals() async {
final data = await SQLHelper.getItems();
setState(() {
_journals = data;
_isLoading = false;
});
}
@override
void initState() {
super.initState();
_refreshJournals(); // Loading the diary when the app starts
}
TextEditingController _nameController = new TextEditingController();
TextEditingController _mobileController = new TextEditingController();
TextEditingController _adressController = new TextEditingController();
// This function will be triggered when the floating button is pressed
// It will also be triggered when you want to update an item
void _showForm(int? id) async {
if (id != null) {
// id == null -> create new item
// id != null -> update an existing item
final existingJournal =
_journals.firstWhere((element) => element['id'] == id);
_nameController.text = existingJournal['name'];
_mobileController.text = existingJournal['mobile'];
_adressController.text = existingJournal['adress'];
}
// Insert a new journal to the database
Future<void> _addItem() async {
await SQLHelper.createItem(
_nameController.text, _mobileController.text, _adressController.text);
_refreshJournals();
}
// Update an existing journal
Future<void> _updateItem(int id) async {
await SQLHelper.updateItem(id, _nameController.text,
_mobileController.text, _adressController.text);
_refreshJournals();
}
// Delete an item
void _deleteItem(int id) async {
await SQLHelper.deleteItem(id);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Successfully deleted a journal!'),
));
_refreshJournals();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Jaber'),
),
body: Container(
padding: EdgeInsets.all(15),
width: double.infinity,
height: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextField(
controller: _nameController,
decoration: InputDecoration(hintText: 'Name'),
),
SizedBox(
height: 10,
),
TextField(
controller: _mobileController,
decoration: InputDecoration(hintText: 'mobile'),
keyboardType: TextInputType.number,
),
TextField(
controller: _adressController,
decoration: InputDecoration(hintText: 'adress'),
),
SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () async {
// Save new journal
if (id == null) {
await _addItem();
}
if (id != null) {
await _updateItem(id);
}
// Clear the text fields
_nameController.text = '';
_mobileController.text = '';
_adressController.text = '';
// Close the bottom sheet
Navigator.of(context).pop();
},
child: Text(id == null ? 'Create New' : 'Update'),
)
],
),
));
}
}
}
这是我的代码,但我从不更改页面。错误 = 未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称。
解决方案
推荐阅读
- css - 顶部菜单问题的间距
- c - Pairing even and odd numbers in an array
- ios - 图像未在方向更改时调整大小
- angular - 角度测试:FormControl valueChanges Observable
- mysql - 如何使用另一列作为乘数而不是行数 MySQL 从表中获取最高出现值
- networking - 管理员可以劫持我的家用电脑吗
- java - 在 Java 8 中将日期从 ISO 8601 Zulu 字符串转换为 java.time.Instant
- c# - WPF 使用 StoryBoard 如何在每次点击时重置样式
- python - 查找python pandas中连续重复元素的计数
- plsql - PLS-00302:必须声明组件“EXISTS”