简体   繁体   中英

can't pass userdata through SearchDelegate class in flutter

In my search page, I have userdata class/object (including username, useraddress, etc) that I want to get from the former page and then pass to the next page, but my Search page doesn't pass userdata class/object to the next page. In order to make it work, I wrote the same code as I did with typical StatelessWidget like below, but failed. In this search page, how can I get userdata from the former page and then pass it to the next page? Here is my code. In the former page(statefulWidget), I made an iconButton to go to the search page like this;

  IconButton(icon: Icon(Icons.search),
           onPressed: () async {await showSearch(context: context, 
           delegate: DataSearch(***userdata: userdata***));
                            })

And then, I made DataSearch class to implement search on a page;

  class DataSearch extends SearchDelegate<String> {
   ***final Userdata userdata;
    DataSearch({this.userdata});***

   @override
   List<Widget> buildActions(BuildContext context) {
       return   ...     }

   @override
   Widget buildLeading(BuildContext context) {
   return    ...      }

   @override
   Widget buildResults(BuildContext context) {
         ...         }

   @override
   Widget buildSuggestions(BuildContext context) {
     return ListView(
            .....
            onTap: () async {
              if (afields.contains(e)) {
                await Navigator.push(context,
                    MaterialPageRoute(builder: (context) => 
                      NextPage(***userdata: userdata***)));} 
 

I got able to pass the userdata object through this search page to the next page by creating stateful widget instead of the search delegate class posted above.

//part of statefulWidget  
class _DataSearchState extends State<DataSearch> {
TextEditingController _searchController = TextEditingController();

@override
void initState() {
  super.initState();
  _searchController.addListener(_onSearchChanged);
}

@override
void dispose() {
  _searchController.removeListener(_onSearchChanged);
  _searchController.dispose();
  super.dispose();
}

Widget build(BuildContext context) {
 return Scaffold(
  body: Column(
    children: [
       TextField(
          controller: _searchController,
          decoration: InputDecoration(prefixIcon: Icon(Icons.search)),
        ),
      ),
     ListView.builder(
          itemCount: resultsList.length,
          itemBuilder: (BuildContext context, int ndx) {
            return ListTile(
                title: Text(resultsList[ndx]),
                onTap: () {
                  Navigator.push(context, 
    MaterialPageRoute(builder: (context) => NextPage(userdata: userdata)));
                });
          }
       }

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