0

In my flutter application I have a button for follow and unfollow a user.

 buttonstate.isSelected && widget.user!.isFollowing == true
                      ? ElevatedButton(
                          onPressed: () async {
                            await unfollowuser(widget.user!.id); //Api Call
                          },
                          child: Text(
                            'Following',
                            style: TextStyle(
                              color: Theme.of(context).disabledColor,
                            ),
                          ),
                        )
                      : ElevevatedButton(
                          onPressed: () async {
                            buttonstate.setButtonState(true);
                            await followuser(widget.user!.id); //Api Call
                          },
                          child: Text(
                            'Follow',
                            style: TextStyle(
                              color: Theme.of(context).accentColor,
                            ),
                          ),
                        ),

My objective is whenever the button is pressed I want the Api call to happen as well as the state of the button should change from 'Follow' to 'Following'. Now the Api gets called, but the button wont change to 'following'. After the page is refreshed, button changes to 'following'. How can i manage state of the button using provider??

Ive created a provider for this purpose as follows

import 'package:flutter/material.dart';

class ButtonProvider with ChangeNotifier {
  bool isSelected = false;

  void setButtonState(bool value) {
    isSelected = value;
    notifyListeners();
  }

  bool? get buttondata {
    return isSelected;
  }
}
Febin Johnson
  • 277
  • 1
  • 6
  • 21

0 Answers0