I'm new to flutter and really stuck.I am trying to create a search function that calls a list in my Sqfite database, using a keyword. The database all works fine, but when I insert a keyword in my searchbar that part of the app crashes with the following error:
LateInitializationError: Field 'insertFunction' has not been initialized.Im using the insertFunction and deleteFunction parameters and the constructor, but my createState throws an error then that it needs insert and deleteFunction data, which I cannto do as one cannot pass logic into createState.
Any help will be much appreciated. I need to solve my createState problem and my Lateinitialization problem.
Here is the code:
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'),
);
}
}),
],
),
),
);
}
}
CodePudding user response:
This is how your code should be
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