Pixxu Waku
Pixxu Waku

Reputation: 443

How to call a method when navigating back from another screen in flutter?

I have a list view of items. For each item in the list, there is an edit option, when on click that im navigating from ListScreen to the EditScreen. Edit Screen has a static update button but the editing view is changed according to the item selected to be edited From the ListScreen.

Now when I update the items in Edit screen, and press update button I can navigate to ListScreen back but I need the list view to rebuild on that. The function to build the list is given inside of a FutureBuilder in ListScreen it self. In the initial navigate to the ListScreen im calling that method in initState().

Is there a way that I can manage this?

ListScreen

//provider
late AddItemProvider? _updateItemProvider;

@override
  void initState() {
    _dataFuture = _getAddedData().whenComplete(() => refresh());
    super.initState();
  }

void _gotoEditAccess(BuildContext ctx, String title) async {
    var nav =
        Navigator.pushNamed(ctx, suggestionUpdateUIRoute, arguments: title);
    // of(context)
    if (nav != null && nav == true) {
      await _dataFuture; //_getAddedData().whenComplete(() => refresh());
    }
  }

//in the body I have this list view
//added item is the list view item which holds Function parameters for onEdit and onDelete
//used as onTap: () => widget.onEdit!(),

ListView.builder(
                        shrinkWrap: true,
                        itemCount: _listOfItems.length,
                        itemBuilder: (context, index) {
                          return AddedItem(
                            icon: _listOfItems[index].icon,
                            title: _listOfItems[index].title,
                            content: _listOfItems[index].content,
                            onEdit: () async {
                              _gotoEditAccess(
                                  context, _listOfItems[index].title!);
                            },
                            onDelete: () {
                              _deleteItem(_listOfItems[index].title!);
                            },
                          );
                        },
                      ),

edit screen have a base view with common update button and the body is being replaced with a actual editing view matching what needs to be edited.

EditScreen

@override
  Widget build(BuildContext context) {
    _updateItemProvider = Provider.of<AddItemProvider>(context, listen: false);
...
//rest of the body for matched editing screen...

//inside the base view of edit screen

_goBack() {
    Navigator.pop(context, true);}

@override
  Widget build(BuildContext context) {
    _updateItemProvider = Provider.of<AddItemProvider>(context, listen: false);

_submitBtn() => Consumer<AddItemProvider>(
          builder: (_, AddItemProvider updateItemProvider, __) {
            _updateItemProvider = updateItemProvider;
            return ButtonWidget(
              btnColor: CustomColors.green600,
              borderColor: CustomColors.green600,
              textColor: CustomColors.mWhite,
              text: "Update",
              eButtonType: eButtonType.bText,
              eButtonState: _submitBtnState,
              onPressed: () async {
                await saveSelectedList(updateItemProvider.updateItems!);
                _updateItemProvider!.removeAll();
                _goBack();
              },
            );
          },
        );

Upvotes: 0

Views: 4260

Answers (1)

Ravi Limbani
Ravi Limbani

Reputation: 1172

You need to write this code were you want to navigate

onTap:() async{
                var test = await Navigator.of(context).push(
                          MaterialPageRoute(builder: (context) =>EditProfile()));
   
                if(test != null || test == true){
                  // perform your function
                 }
           }

You need to pass any content when navigate back from edit screen

 Navigator.pop(context, true);

Upvotes: 3

Related Questions