我得到这个错误:flutter (18022): Closure: () =>Null。我创建了构造函数变量ondeletefunction;ItemsCarWidget类中。然后我从WordListPage类中得到构造函数。但是我不能访问这个函数,我得到了一个错误。如何解决此错误?我不能删除。如何解决这个错误?
代码在这里:
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();
}
}
这不是对函数的调用:
widget.onDeletePress;
为了调用函数,你应该使用:
widget.onDeletePress();
,但是如果你只是使用这个函数的话会更简洁:
onPressed: widget.onDeletePress,