LateInitializationError: Field 'insertFunction has not been initialized
LateInitializationError: Field 'insertFunction has not been initialized
我是 Flutter 的新手,实际上 stuck.I 我正在尝试创建一个搜索功能,使用关键字调用我的 Sqfite 数据库中的列表。数据库一切正常,但是当我在搜索栏中插入关键字时,应用程序的一部分崩溃并出现以下错误:
LateInitializationError: Field 'insertFunction' 尚未 initialized.Im 使用 insertFunction 和 deleteFunction 参数和构造函数,但我的 createState 抛出一个错误,然后它需要 insert 和 deleteFunction 数据,我不能这样做因为不能将逻辑传递到 createState。
任何帮助将不胜感激。我需要解决我的 createState 问题和 Lateinitialization 问题。
代码如下:
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();//createState is throwing an error and
as that I insert insertFunction and
deleteFunction here. But if i do, it
also say I should not put logic into
createState.
}
class _SearchPageState extends State<SearchPage> {
late final Function insertFunction;
late final Function deleteFunction;
var db = DatabaseConnect();
String keyword = '';
_SearchPageState({required this.insertFunction,required this.deleteFunction,});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Search My Clients'),
leading: GestureDetector(
onTap: () {
Navigator.of(context).pushReplacementNamed('/homePage');
},
child: const Icon(
Icons.arrow_back,
),
),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15.0),
child: TextField(
autofocus: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'keyword',
prefixIcon: Icon(Icons.search)),
onChanged: (value) {
keyword = value;
setState(() {});
},
),
),
FutureBuilder(
future: db.searchContacts(keyword),
builder: (context, AsyncSnapshot<List> snapshot) {
if (snapshot.hasError) const Text('error');
var data = snapshot.data;
var datalength = data!.length;
if (snapshot.hasData) {
return datalength == 0
? const Center(
child: Text('no data found'),
)
: Container(
child: LimitedBox(
maxHeight: 200,
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: datalength,
shrinkWrap: true,
itemBuilder: (context, i) => CustomerCard(
id: data[i].id,
title: data[i].title,
name: data[i].name,
phone: data[i].phone,
fax: data[i].fax,
email: data[i].email,
street: data[i].street,
city: data[i].city,
town: data[i].town,
code: data[i].code,
isExpanded: data[i].isExpanded,
insertFunction: insertFunction,
deleteFunction: deleteFunction,
),
),
),
);
} else {
return const Center(
child: Text('no data found'),
);
}
}),
],
),
),
);
}
}
这就是您的代码应有的样子
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
insertFunction(){
//function body goes here
}
deleteFunction(){
//function body goes here
}
var db = DatabaseConnect();
String keyword = '';
//the rest goes here
我是 Flutter 的新手,实际上 stuck.I 我正在尝试创建一个搜索功能,使用关键字调用我的 Sqfite 数据库中的列表。数据库一切正常,但是当我在搜索栏中插入关键字时,应用程序的一部分崩溃并出现以下错误:
LateInitializationError: Field 'insertFunction' 尚未 initialized.Im 使用 insertFunction 和 deleteFunction 参数和构造函数,但我的 createState 抛出一个错误,然后它需要 insert 和 deleteFunction 数据,我不能这样做因为不能将逻辑传递到 createState。
任何帮助将不胜感激。我需要解决我的 createState 问题和 Lateinitialization 问题。
代码如下:
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();//createState is throwing an error and
as that I insert insertFunction and
deleteFunction here. But if i do, it
also say I should not put logic into
createState.
}
class _SearchPageState extends State<SearchPage> {
late final Function insertFunction;
late final Function deleteFunction;
var db = DatabaseConnect();
String keyword = '';
_SearchPageState({required this.insertFunction,required this.deleteFunction,});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Search My Clients'),
leading: GestureDetector(
onTap: () {
Navigator.of(context).pushReplacementNamed('/homePage');
},
child: const Icon(
Icons.arrow_back,
),
),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15.0),
child: TextField(
autofocus: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'keyword',
prefixIcon: Icon(Icons.search)),
onChanged: (value) {
keyword = value;
setState(() {});
},
),
),
FutureBuilder(
future: db.searchContacts(keyword),
builder: (context, AsyncSnapshot<List> snapshot) {
if (snapshot.hasError) const Text('error');
var data = snapshot.data;
var datalength = data!.length;
if (snapshot.hasData) {
return datalength == 0
? const Center(
child: Text('no data found'),
)
: Container(
child: LimitedBox(
maxHeight: 200,
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: datalength,
shrinkWrap: true,
itemBuilder: (context, i) => CustomerCard(
id: data[i].id,
title: data[i].title,
name: data[i].name,
phone: data[i].phone,
fax: data[i].fax,
email: data[i].email,
street: data[i].street,
city: data[i].city,
town: data[i].town,
code: data[i].code,
isExpanded: data[i].isExpanded,
insertFunction: insertFunction,
deleteFunction: deleteFunction,
),
),
),
);
} else {
return const Center(
child: Text('no data found'),
);
}
}),
],
),
),
);
}
}
这就是您的代码应有的样子
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
insertFunction(){
//function body goes here
}
deleteFunction(){
//function body goes here
}
var db = DatabaseConnect();
String keyword = '';
//the rest goes here