skip to Main Content

hi and hello everyone ,

i am new with flutter and dart

and i am start using isar database
and everything is good and ok

but i have problem with Search

i create function for search

  getAll(String search) async* {
    final isar = await db;
    final query = isar.books
        .where()
        .filter()
        .titleContains(search)
        .build();

    await for (final results in query.watch(fireImmediately: true)) {
      if (results.isNotEmpty) {
        yield results;
      }
    }
  }

and i add Search i home screen like this in TextField :

onChanged: (value) {
isarService.getAll(search: value);
}

,

but not work with me i try everthing but no work

soory for my english

2

Answers


  1. maybe you should wait for the response:

    onChanged: (value) async {
    await isarService.getAll(search: value);
    }
    
    Login or Signup to reply.
  2. Inside your getAllBooks() just remove .build()

    Stream<List<Book>> getAllBooks({String? search}) async* {
    print(search);
    final isar = await db;
    final query = isar.books
        .where()
        .filter()
        .titleContains(search ?? '', caseSensitive: false);
    
    await for (final results in query.watch(fireImmediately: true)) {
      if (results.isNotEmpty) {
        yield results;
      }
    }
    }
    

    Convert your book_list_screen.dart class in StatefulWidget, and use a variable.

    String search = ""; //for searching
    

    Inside your TextField’s onChanged()

    onChanged: (value) {
                
        setState(() {
                    search = value;
                  });
                 
                },
    

    and inside your Expanded widget use something like this

    Expanded(
        child: StreamBuilder<List<Book>>(
              stream: widget.isarService.getAllBooks(search: search),
      builder: (context, snapshot) {
          ...
      }
                ),
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search