首页 > 解决方案 > 缺少(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'),
                )
              ],
            ),
          ));
    }
  }
}

这是我的代码,但我从不更改页面。错误 = 未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称。

标签: flutterdart

解决方案


推荐阅读