How set provider to only one type of data in flutter?

Issue

    class UserProvider extends ChangeNotifier {
          UserLoginModel userLoginModel = UserLoginModel();
                UserLoginModel get userdata {
         return userLoginModel;
          }

           void addlogindata(UserLoginModel data) {
          userLoginModel = data;
            notifyListeners();
         }
              }

How to set data to this class using api or UserLoginModel through function
i want to set data like this

  if (response.statusCode == 200) {
  print(response.body);
  var data = UserLoginModel.fromJson(jsonDecode(response.body)); userprovider.addlogindata(data);

bur i am getting error by doing this kind of thing

Solution

var data=Provider.of<UserProvider>(context,listen:false);
var newdata=data.addlogindata(responsedata);

that work perfectly for me and now i can use that data anywhere in the app

Answered By – Ali Hassan

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *