flutterasync-awaitnullnavigatorissuu

Null value return from push


When I click button The value is the return value of the Navigator.push() - method. "because pressing the Button which returns null, does not make the Future go null, but its content. This leads to the unintentional behaviour"

enter image description here home Screen

PopupMenuButton(
            icon: Icon(Icons.more_vert,color: Colors.black,),
            offset: Offset(0, 40),
            itemBuilder: (context) => [
              PopupMenuItem(
                child: Text('View Cart'),
                onTap: () async {
                final result = await Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => CartScreen(cart: cart)),
                  ) ;
                  if(result)
                  {
                    setState(() {
                      
                    });
                  }
                },
              ),

CartScreen

 WillPopScope(

      onWillPop: () {
        Navigator.pop(context,true);

         return new Future(() => false);;
      },
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          iconTheme: IconThemeData(color: Colors.black),
          elevation: 0,
        ),

when i click the button to navigate page


Solution

  • I was trying to use the onSelected property

    PopupMenuButton<int>(
                          onSelected: (result) {
                            if (result == 1) {
                              nav(context, snap.data![index]);
                            }
                          },
                          icon: Icon(Icons.more_vert_outlined),
                          itemBuilder: (context) => [
                            PopupMenuItem(
                                onTap: () async {
                                 
                                },
                                value: 1,
                                child: Text("Edit")),
                            PopupMenuItem(
                                onTap: () async {
                                  Map<String, dynamic>? res = await api
                                      .deletepost(snap.data![index].id!);
                                  ScaffoldMessenger.of(context).showSnackBar(
                                      SnackBar(
                                          backgroundColor: Colors.amber,
                                          content:
    Text(
                                      " ${snap.data![index].id} isdeleted ")));
                                },
                                value: 2,
                                child: Text("Delete")),
                          ],
                        ),
                     
    
    
    nav(BuildContext context, TODO td) async {
    API api = API();
    List<dynamic>? data = await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => UpdateTodo(
                td: td,
              )));
    print(data);
    
    if (data != null) {
    var res = await api.updatePost(data);
    print(res);
    
    ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(backgroundColor: Colors.amber, content: Text("Edit 
    succes")));
    }
    }
    

    Note: Popup routes Routes don't have to obscure the entire screen. [PopupRoute]s cover the screen with a [ModalRoute.barrierColor] that can be only partially opaque to allow the current screen to show through. Popup routes are "modal" because they block input to the widgets below.

    There are functions which create and show popup routes. For example: [showDialog], [showMenu], and [showModalBottomSheet]. These functions return their pushed route's Future as described above. Callers can await the returned value to take an action when the route is popped, or to discover the route's value.enter code here

    There are also widgets which create popup routes, like [PopupMenuButton] and [DropdownButton]. These widgets create internal subclasses of PopupRoute and use the Navigator's push and pop methods to show and dismiss them.