farhang fallah
farhang fallah

Reputation: 23

update image in state in flutter

i have a listView.builder where i show my data from api. When I onTap on a GestureDetector from the list item, I send data to api. After returning the data I would like to change my list item. but i can't this.

for example my state one item is like this :

new Container(
  margin: EdgeInsets.only(right: 10, top: 15),
  child: new GestureDetector(
    child: isFavorite == 1 ? 
      new Image.asset(
      "assets/images/favorite.png",
      fit: BoxFit.contain,
      height: 35,
      width: 70,
      )
      : new Image.asset(
      "assets/images/unfavorite.png",
      fit: BoxFit.contain,
      color: Colors.white,
      height: 35,
      width: 70,
    ),

     onTap: () async {
       if (isLogin == true){
         if(isFavorite == 0) {
           getIsLikeAccommodation(accommodation.id , 1 );
         }
       else {
       getIsLikeAccommodation( accommodation.id , 0 );
     }
   }
  else {
     // safeye vorod be narm afzar bala biad
       }
     },
 )
),

my connect to server :

getIsLikeAccommodation(int _accommodation_id , int _kind ) async {

 if(await checkConnectionInternet()) {
  print(_accommodation_id);
  Map response = await AccommodationService().getIsLikeAccommodation({ "accommodation_id" : _accommodation_id , "is_favorite" : _kind});
  var _responsCode = response['code'];
  var _isLikeResponse = response['accommodation']['is_favorite'];
  if (_responsCode == '1002'){

    After this condition is true , I want the state condition to be checked again and the view changed
  }


  } else {
    _scaffoldKey.currentState.showSnackBar(
      new SnackBar(
        content: new GestureDetector(

          child: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              new Text('از اتصال دستگاه به اینترنت مطمئن شوید', style: TextStyle(fontFamily: 'Shabnam')),
              new Icon(Icons.wifi_lock , color: Colors.white)
            ],
          ),
        )
      )
    );
  }
}

Future<bool> checkConnectionInternet() async {
  var connectivityResult = await (new      Connectivity().checkConnectivity());
  return connectivityResult == ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi;
 }
}

After condition

if (_responsCode == '1002')

is true I , want the state condition to be checked again and the view changed

Upvotes: 0

Views: 3454

Answers (1)

Erkan
Erkan

Reputation: 766

if i understood correctly, you need the call setState function after change the condition.

https://api.flutter.dev/flutter/widgets/State/setState.html

Upvotes: 1

Related Questions