简体   繁体   中英

Search through sqflite database (flutter) with a Searchbar

I am looking for a solution to search through a sqflite database-table via a searchbar. I created a "customer" table in my database and if I enter a firstname ("vorname" in my code - german) that correlates with a database entry into the searchbar, I would like that it gets displayed in a ListView (I figured out how to return a ListView as results through tutorials)

I have a function getCustomers which returns all properties of a customer - that might be helpful.

I thought of something whith a Databasehelper that gets the table customers in my Database or all firstnames (vorname) as a list and I can compare if the input string in the searchbar correlates with a String in that list. Probably something with a FutureBuilder too. I do not know how to execute this, of course I am open to any solution. Any help would be appreciated - if you need more code let me know please.

getCustomers() in the file where I also create the database:

Future<List<Customer>> getCustomers() async {
    Database _db = await database();
    List<Map<String, dynamic>> customerMap = await _db.query('customers');
    return List.generate(customerMap.length, (index){
      return Customer(
        id: customerMap[index]['id'],
        vorname: customerMap[index]['vorname'],
        nachname: customerMap[index]['nachname'],
        telefonnummer: customerMap[index]['telefonnummer'],
        email: customerMap[index]['email']
      );
    });
  }

The code I have (from tutorial) - all in the same file, that has no real functionality other than showing that searchbar works:

Function that creates outcome:

    await Future.delayed(Duration(seconds: 2));
    return List.generate(10, (int index) {
      return Post(
        "Vorname $index",
        "Nachname $index",
      );
    });
  }

Post class:


class Post {
  final String vorname;
  final String nachname;

  Post(this.vorname, this.nachname);
}

Search-Bar:

SearchBar(
           onSearch: search,
              onItemFound: (Post post, int index) {
                 return ListTile(
                   title: Text(post.vorname),
                    subtitle: Text(post.nachname),
                 );
               },
             )

You can try this...

 Future<List<Customer>> searchCustomer(String name) async{
    var res = await db.query(
      "customers",
      where: "vorname LIKE ?"
      whereArgs: ['%$name']
     );
    List<Customer> customerList = res.map((element)=>Customer(
            id: element[index]['id'],
            vorname: element['vorname'],
            nachname: element['nachname'],
            telefonnummer: element['telefonnummer'],
            email: element['email']
          ));
    return customerList;
    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM