skip to Main Content

i create search icon in appbar to open search and when i click on it there is error appear
the error is "Navigator operation requested with a context that does not include a Navigator"
and it’s my code

void main() {
  runApp(rekomo2());
}

class rekomo2 extends StatefulWidget {
  rekomo2({super.key});

  @override
  State<rekomo2> createState() => _rekomo2State();
}
class _rekomo2State extends State<rekomo2> {
  int curindex = 0;
  
  final screen = [chats(), groups(), nearme(), status(), favourite()];
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

        //  routes: {"nearme": (context) => nearme()},
        debugShowCheckedModeBanner: false,
        home: MaterialApp(
            
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              floatingActionButton: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  FloatingActionButton(
                    backgroundColor: Colors.purple[300],
                    onPressed: () {
                      //  showSearch(context: context , delegate: search(),);
                    },
                    child: const Icon(
                      Icons.search,
                      color: Colors.white,
                      size: 30,
                    ),
                  ),
                  Container(
                    height: 10,
                  ),
                  FloatingActionButton(
                    backgroundColor: Colors.purple[300],
                    onPressed: () {},
                    child: const Icon(
                      Icons.add,
                      color: Colors.white,
                      size: 30,
                    ),
                  ),
                ],
              ),
              appBar: AppBar(
                actions: [
                  IconButton(
                      onPressed: () {
                        showSearch(context: context, delegate: search());
                      },
                      icon: Icon(Icons.search, size: 30, color: Colors.purple[300]))
                ],
                leading: Builder(
                    builder: (context) => IconButton(
                          onPressed: () {
                            Scaffold.of(context).openDrawer();
                          },
                          icon: const Icon(Icons.menu),
                          color: Colors.purple[300],
                        )),
                backgroundColor: Colors.white,
                title: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "Rekomo2",
                      style: TextStyle(
                          fontSize: 25, fontWeight: FontWeight.bold, color: Colors.purple[300]),
                    ),
                    
              bottomNavigationBar: BottomNavigationBar(
                  currentIndex: curindex,
                  onTap: (value) {
                    setState(() {
                      curindex = value;
                    });
                  },
                  selectedItemColor: Colors.purple[600],
                  // currentIndex: screen[Key.],
                  type: BottomNavigationBarType.fixed,
                  showSelectedLabels: true,
                  showUnselectedLabels: false,
                  items: const [
                    BottomNavigationBarItem(icon: Icon(Icons.chat), label: "Chats"),
                    BottomNavigationBarItem(
                      icon: Icon(Icons.group),
                      label: "Groups",
                    ),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.location_on_outlined), label: "People Near Me"),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.switch_access_shortcut_sharp), label: "Status"),
                    BottomNavigationBarItem(icon: Icon(Icons.star), label: "Favourite"),
                  ]),
              //  backgroundColor: Colors.red,
              drawer: const Drawer(),
              body: screen[curindex],
            )));
  }
}

class search extends SearchDelegate {
  List searchr = ["devil", "karma", "reem", "ahmed", "moaa"];
  @override
  List<Widget>? buildActions(BuildContext context) {
    return [
      IconButton(
          onPressed: () {
            query = "";
          },
          icon: Icon(Icons.close))
    ];
  }

  @override
  Widget? buildLeading(BuildContext context) {
    return IconButton(
        onPressed: () {
          query = "";
        },
        icon: Icon(Icons.search));
  }

  @override
  Widget buildResults(BuildContext context) {
    return Text("data");
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    if (query.isEmpty) {
      return Text("");
    } else {
      List filterlist = searchr.where((element) => element.contains(query)).toList();
      return ListView.builder(
        itemCount: filterlist.length,
        itemBuilder: (context, i) {
          return InkWell(onTap: () => context, child: Card(child: Text(filterlist[i])));
        },
      );
    }
  }
}

and i don’t know what cause the error and how to fix it
and i try to use search delegate with floating button but the same error appear

2

Answers


  1. Chosen as BEST ANSWER

    the error disappear after add builder and hero code :

        Widget build(BuildContext context) {
        return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              floatingActionButton: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Builder(builder: (context) {
                    return FloatingActionButton(
                      heroTag: 'searchBtn',
                      backgroundColor: Colors.purple[300],
                      onPressed: () {
                        showSearch(
                          context: context,
                          delegate: search(),
                        );
                      },
                      child: const Icon(
                        Icons.search,
                        color: Colors.white,
                        size: 30,
                      ),
                    );
                  }),
                  Container(
                    height: 10,
                  ),
                  FloatingActionButton(
                    heroTag: 'addBtn',
                    backgroundColor: Colors.purple[300],
                    onPressed: () {},
                    child: const Icon(
                      Icons.add,
                      color: Colors.white,
                      size: 30,
                    ),
                  ),
                ],
              ),
              appBar: AppBar(
                actions: [
                  Builder(
                    builder: (context) => IconButton(
                      onPressed: () {
                        showSearch(context: context, delegate: search());
                      },
                      icon: Icon(Icons.search, size: 30, color: Colors.purple[300]),
                    ),
                  )
                ],
                leading: Builder(
                    builder: (context) => IconButton(
                          onPressed: () {
                            Scaffold.of(context).openDrawer();
                          },
                          icon: const Icon(Icons.menu),
                          color: Colors.purple[300],
                        )),
                backgroundColor: Colors.white,
                title: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "Rekomo2",
                      style: TextStyle(
                          fontSize: 25, fontWeight: FontWeight.bold, color: Colors.purple[300]),
                    ),
                  ],
                ),
              ),
              bottomNavigationBar: BottomNavigationBar(
                  currentIndex: curindex,
                  onTap: (value) {
                    setState(() {
                      curindex = value;
                    });
                  },
                  selectedItemColor: Colors.purple[600],
                  type: BottomNavigationBarType.fixed,
                  showSelectedLabels: true,
                  showUnselectedLabels: false,
                  items: const [
                    BottomNavigationBarItem(icon: Icon(Icons.chat), label: "Chats"),
                    BottomNavigationBarItem(
                      icon: Icon(Icons.group),
                      label: "Groups",
                    ),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.location_on_outlined), label: "People Near Me"),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.switch_access_shortcut_sharp), label: "Status"),
                    BottomNavigationBarItem(icon: Icon(Icons.star), label: "Favourite"),
                  ]),
              drawer: const Drawer(),
              body: screen[curindex],
            ));
      }
    }
    
    class search extends SearchDelegate {
      List searchr = ["devil", "karma", "reem", "ahmed", "moaa"];
      @override
      List<Widget>? buildActions(BuildContext context) {
        return [
          IconButton(
              onPressed: () {
                query = "";
              },
              icon: Icon(Icons.close))
        ];
      }
    
      @override
      Widget? buildLeading(BuildContext context) {
        return IconButton(
            onPressed: () {
              close(context, null);
            },
            icon: Icon(Icons.search));
      }
    
      @override
      Widget buildResults(BuildContext context) {
        return Text("data");
      }
    
      @override
      Widget buildSuggestions(BuildContext context) {
        if (query.isEmpty) {
          return Text("");
        } else {
          List filterlist = searchr.where((element) => element.contains(query)).toList();
          return ListView.builder(
            itemCount: filterlist.length,
            itemBuilder: (context, i) {
              return InkWell(onTap: () => context, child: Card(child: Text(filterlist[i])));
            },
          );
        }
      }
    }
    
    

  2. I have reviewed your code and found that you were trying to use the showSearch function with a context that does not have a Navigator in its widget tree.

    In your code, you are using a nested MaterialApp, which can lead to issues. There should only be one MaterialApp at the root of your widget tree. Here’s how you can fix your code:

    1. Remove the nested MaterialApp.
    2. Use showSearch with the correct context.

    I have modified your code with some additions, feel free to makes changes and see if it works.

    Code:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(Rekomo2());
    }
    
    class Rekomo2 extends StatefulWidget {
      Rekomo2({super.key});
    
      @override
      State<Rekomo2> createState() => _Rekomo2State();
    }
    
    class _Rekomo2State extends State<Rekomo2> {
      int curindex = 0;
      final screen = [Chats(), Groups(), NearMe(), Status(), Favourite()];
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            floatingActionButton: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                FloatingActionButton(
                  backgroundColor: Colors.purple[300],
                  onPressed: () {
                    showSearch(context: context, delegate: Search());
                  },
                  child: const Icon(
                    Icons.search,
                    color: Colors.white,
                    size: 30,
                  ),
                ),
                Container(
                  height: 10,
                ),
                FloatingActionButton(
                  backgroundColor: Colors.purple[300],
                  onPressed: () {},
                  child: const Icon(
                    Icons.add,
                    color: Colors.white,
                    size: 30,
                  ),
                ),
              ],
            ),
            appBar: AppBar(
              actions: [
                IconButton(
                    onPressed: () {
                      showSearch(context: context, delegate: Search());
                    },
                    icon: Icon(Icons.search, size: 30, color: Colors.purple[300])),
              ],
              leading: Builder(
                builder: (context) => IconButton(
                  onPressed: () {
                    Scaffold.of(context).openDrawer();
                  },
                  icon: const Icon(Icons.menu),
                  color: Colors.purple[300],
                ),
              ),
              backgroundColor: Colors.white,
              title: Text(
                "Rekomo2",
                style: TextStyle(
                    fontSize: 25, fontWeight: FontWeight.bold, color: Colors.purple[300]),
              ),
            ),
            bottomNavigationBar: BottomNavigationBar(
              currentIndex: curindex,
              onTap: (value) {
                setState(() {
                  curindex = value;
                });
              },
              selectedItemColor: Colors.purple[600],
              type: BottomNavigationBarType.fixed,
              showSelectedLabels: true,
              showUnselectedLabels: false,
              items: const [
                BottomNavigationBarItem(icon: Icon(Icons.chat), label: "Chats"),
                BottomNavigationBarItem(icon: Icon(Icons.group), label: "Groups"),
                BottomNavigationBarItem(icon: Icon(Icons.location_on_outlined), label: "People Near Me"),
                BottomNavigationBarItem(icon: Icon(Icons.switch_access_shortcut_sharp), label: "Status"),
                BottomNavigationBarItem(icon: Icon(Icons.star), label: "Favourite"),
              ],
            ),
            drawer: const Drawer(),
            body: screen[curindex],
          ),
        );
      }
    }
    
    class Search extends SearchDelegate {
      List searchResults = ["devil", "karma", "reem", "ahmed", "moaa"];
    
      @override
      List<Widget>? buildActions(BuildContext context) {
        return [
          IconButton(
            onPressed: () {
              query = "";
            },
            icon: Icon(Icons.close),
          ),
        ];
      }
    
      @override
      Widget? buildLeading(BuildContext context) {
        return IconButton(
          onPressed: () {
            close(context, null);
          },
          icon: Icon(Icons.arrow_back),
        );
      }
    
      @override
      Widget buildResults(BuildContext context) {
        return Center(child: Text(query));
      }
    
      @override
      Widget buildSuggestions(BuildContext context) {
        final suggestions = query.isEmpty
            ? []
            : searchResults.where((element) => element.contains(query)).toList();
    
        return ListView.builder(
          itemCount: suggestions.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(suggestions[index]),
              onTap: () {
                query = suggestions[index];
                showResults(context);
              },
            );
          },
        );
      }
    }
    
    class Chats extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(child: Text('Chats'));
      }
    }
    
    class Groups extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(child: Text('Groups'));
      }
    }
    
    class NearMe extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(child: Text('People Near Me'));
      }
    }
    
    class Status extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(child: Text('Status'));
      }
    }
    
    class Favourite extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(child: Text('Favourite'));
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search