How to implement search delegate with sqflite flutter

857 views
Skip to first unread message

mahantappa b k

unread,
May 25, 2019, 7:32:38 AM5/25/19
to Flutter Dev
I have created an app that stores some notes in sqlite database. I did all CRUD operations and it's working well, but when I'm trying to make search operation inside my database with SearchDelegate, I am not getting any data in search bar . My code is here please go through it and help.I am stuck since 4 days.

I think my query is in suggestion is wrong please help me writing query.


Andy Greenshaw

unread,
May 25, 2019, 8:26:51 AM5/25/19
to Flutter Dev, mahantappa b k
Shouldn't your constructor be 'DataSearch(this.items);' not ' DataSearch(this.suggestion);'? As you have it, you are never setting 'items' (except to create an empty list), but then do 

final suggestion = query.isEmpty ? items :
    items.where((target) => target.title.startsWith(query)).toList(); 

but 'items' will always be an empty list.
On 25 May 2019, 12:32 +0100, mahantappa b k <mbkum...@gmail.com>, wrote:
I have created an app that stores some notes in sqlite database. I did all CRUD operations and it's working well, but when I'm trying to make search operation inside my database with SearchDelegate, I am not getting any data in search bar . My code is here please go through it and help.I am stuck since 4 days.

I think my query is in suggestion is wrong please help me writing query.


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/9b6531de-9979-412f-b0e8-e0e79d8a70cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mahantappa b k

unread,
May 25, 2019, 8:35:33 AM5/25/19
to Flutter Dev
I have tried it yesterday declaring suggestion in DataSearch(this.suggestion) but it showing an error called ''create getter suggestion"


On Saturday, May 25, 2019 at 5:56:51 PM UTC+5:30, Andy Greenshaw wrote:
Shouldn't your constructor be 'DataSearch(this.items);' not ' DataSearch(this.suggestion);'? As you have it, you are never setting 'items' (except to create an empty list), but then do 

final suggestion = query.isEmpty ? items :
    items.where((target) => target.title.startsWith(query)).toList(); 

but 'items' will always be an empty list.
On 25 May 2019, 12:32 +0100, mahantappa b k <mbkum...@gmail.com>, wrote:
I have created an app that stores some notes in sqlite database. I did all CRUD operations and it's working well, but when I'm trying to make search operation inside my database with SearchDelegate, I am not getting any data in search bar . My code is here please go through it and help.I am stuck since 4 days.

I think my query is in suggestion is wrong please help me writing query.


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Andy Greenshaw

unread,
May 25, 2019, 8:55:44 AM5/25/19
to Flutter Dev, mahantappa b k
This is the code to change:

class DataSearch extends SearchDelegate<Note> {
  DatabaseHelper db = new DatabaseHelper();
  final List<Note> items = new List();
  List<Note> suggestion = new List();
  // ListViewNoteState i = ListViewNoteState();

   DataSearch(this.suggestion);
to

class DataSearch extends SearchDelegate<Note> {
  DatabaseHelper db = new DatabaseHelper();
  final List<Note> items = new List();
  List<Note> suggestion = new List();
  // ListViewNoteState i = ListViewNoteState();

   DataSearch(this.items);

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/bb2e1741-cb4a-45d2-bb2b-72b232fcfcef%40googlegroups.com.
Message has been deleted

mahantappa b k

unread,
May 27, 2019, 12:29:09 AM5/27/19
to Flutter Dev
Thanks Andy it worked for me 

Changes I did is removed final from List<Note> items = new List(); 

class DataSearch extends SearchDelegate<Note> {
  DatabaseHelper db = new DatabaseHelper();
List<Note> items = new List();
  List<Note> suggestion = new List();
  // ListViewNoteState i = ListViewNoteState();

   DataSearch(this.items
);

mahantappa b k

unread,
May 28, 2019, 1:15:06 AM5/28/19
to Flutter Dev
Hey andy I am with another problem, I am getting suggestion list but when I type for searching it only shows first suggestion in the list, not the one which I have searched.
This the code which I am using.

@override
Widget buildSuggestions(BuildContext context) {
final suggestion = query.isEmpty
? items
: items.where((p) => p.title.startsWith(query)).toList();
if(items.isEmpty)
{
print("Null");
}
// return Text('$suggestion');
return ListView.builder(
itemCount: suggestion.length,
itemBuilder: (context, position){
return new ListTile(
title: Text(items[position].title.toString()),
);
}
);
}

Andy Greenshaw

unread,
May 28, 2019, 3:30:53 AM5/28/19
to Flutter Dev, mahantappa b k
Either I need to see all the code, or I can’t really help.

I can see you are using the wrong variable in the building of the list (still). You should be using `suggestion`, not `items`.

return new ListTile(
title: Text(items[position].title.toString()),
);


should be 
return new ListTile(
title: Text(suggestion[position].title.toString()),
);

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/a95f998e-7a21-4db0-85b0-da514de91a1d%40googlegroups.com.

mahantappa b k

unread,
May 28, 2019, 3:33:43 AM5/28/19
to Flutter Dev
Yes, I was wrong now it works
Reply all
Reply to author
Forward
0 new messages