Search code examples
flutterdartcrudsqflite

How to I solve this error (18022): Closure: () => Null


I'm getting this error: flutter (18022): Closure: () => Null. I created constructor variable "function onDeleteFonction" in ItemsCarWidget class. Then I got to my constructor functions from the WordListPage class. But I can't access the function and I get an error. How do I resolve this error? I can't delete.How to I solve this error?

the code is here:

ItemsWidgetCart.dart

class ItemCard extends StatefulWidget {
 Word? word; 
TextEditingController input1;
 TextEditingController input2;
 Function in DeletePress; 
Function? onEditPress;
İtemsWidgetCart.dart
class ItemCard extends StatefulWidget {
  Word? word;
  TextEditingController input1;
  TextEditingController input2;
  Function onDeletePress;
  Function? onEditPress;

  ItemCard(
      {this.word,
        required this.input1,
        required this.input2,
         required this.onDeletePress,
         this.onEditPress});

  @override
  _ItemCardState createState() => _ItemCardState();
}

class _ItemCardState extends State<ItemCard> {
  final DatabaseHelper dbManager = new DatabaseHelper();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Card(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'İngilizce: ${widget.word!.wordEn}',
                    style: TextStyle(fontSize: 15),
                  ),
                  Text(
                    'Türkçe: ${widget.word!.wordTr}',
                    style: TextStyle(
                      fontSize: 15,
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  CircleAvatar(
                    backgroundColor: Colors.white,
                    child: IconButton(
                      onPressed:()=> widget.onEditPress,
                      icon: Icon(
                        Icons.edit,
                        color: Colors.blueAccent,
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 15,
                  ),
                  CircleAvatar(
                    backgroundColor: Colors.white,
                    child: IconButton(
                      onPressed: () {
                        widget.onDeletePress;
                        print("tıklandı word page");
                        print(widget.onDeletePress);
                      } ,
                      icon: Icon(
                        Icons.delete,
                        color: Colors.red,
                      ),),) ],),],),),),);}}

WordListPage.dart

import 'package:your_research_translation/widgets/itemCardWidget.dart';
import 'package:your_research_translation/utils/databasehelper.dart';
class WordListPage extends StatefulWidget {
  const WordListPage({Key? key}) : super(key: key);

  @override
  _WordListPageState createState() => _WordListPageState();
}


class _WordListPageState extends State<WordListPage> {
  late Word word;
  late List<Word> wordList;
  final DatabaseHelper db= new DatabaseHelper();
  TextEditingController input1 = TextEditingController();
  TextEditingController input2 = TextEditingController();
  bool isLoading = false;
  Future refreshNotes() async {
  setState(() => isLoading = true);

  this.wordList = await db.getWordList();

  setState(() => isLoading = false);
}
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:  FutureBuilder(
        future: db.getWordList(),
    builder: (context, AsyncSnapshot snapshot) {
    if (snapshot.hasData){
    wordList = snapshot.data;
    return ListView.builder(
    itemCount: wordList.length,
    itemBuilder: (context, index) {
    Word _word = wordList[index];
    return ItemCard(

    word: _word,
    input1: input1,
    input2: input2,
    onDeletePress:() => db.deleteWord(_word),
    });},;
    } return Center(child: CircularProgressIndicator(),);
    }), ); }}

databaseHelper.dart

import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:your_research_translation/models/word.dart';


class DatabaseHelper{
  late Database _database;
  Future openDb() async {
    _database = await openDatabase(join(await getDatabasesPath(), "word.db"),
        version: 1, onCreate: (Database db, int version) async {
          await db.execute(
            "CREATE TABLE word(id INTEGER PRIMARY KEY autoincrement, wordEn TEXT, wordTr TEXT)",
          );
        });
    return _database;
  }
  Future<int> insertWord(Word word) async {
    await openDb();
    return await _database.insert('word', word.toJson());
  }
  Future<List<Word>> getWordList() async {
    await openDb();
    final List<Map<String, dynamic>> maps = await _database.query('word');

    return List.generate(maps.length, (i) {
      return Word(
          id: maps[i]['id'],
          wordEn: maps[i]['wordEn'],
          wordTr: maps[i]['wordTr']);
    });
    
  }
  Future<int> updateWord(Word word) async {
    await openDb();
    return await _database.update('word', word.toJson(),
        where: "id = ?", whereArgs: [word.id]);
  }
  Future<void> deleteWord(Word word) async {
    await openDb();
     await _database.delete('word', where: "id = ?", whereArgs: [word.id]);
    getWordList();
  }

}

Solution

  • this isn't a call to the function:

    widget.onDeletePress;
    

    in order to call a function, you should use:

    widget.onDeletePress();
    

    but is cleaner if you just asing that function:

    onPressed: widget.onDeletePress,