Kirti, Try this
//Add this import at the top with the others
import mx.binding.utils.ChangeWatcher;
//replace your existing function with this one
private function showStateLegend(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 1, 400);
//This is where we determine if this is the first call to make
//the legend or do we just need to update it
if (legendRepeater.dataProvider != null){
updateLegend();
} else {
showMessage("Please be patient while the Legend is generated...", true);
var liveLayers:Array = getLayers();
var url:String;
for (var i:int = 0; i < liveLayers.length;i++) {
if (liveLayers is ArcGISDynamicMapServiceLayer) {
if (ArcGISDynamicMapServiceLayer(liveLayers).id != "Aerial Photography")
{
requestCount++;
url = ArcGISDynamicMapServiceLayer(liveLayers).url.replace("rest/", "");
webService.generateLegends.send(url, 96, 20, 20, ArcGISDynamicMapServiceLayer(liveLayers).id);
ChangeWatcher.watch(ArcGISDynamicMapServiceLayer(liveLayers), "visibleLayers", updateLegend2);
}
} else if (liveLayers is ArcGISTiledMapServiceLayer) {
requestCount++;
url = ArcGISTiledMapServiceLayer(liveLayers).url.replace("rest/", "");
webService.generateLegends.send(url, 96, 20, 20, ArcGISTiledMapServiceLayer(liveLayers).id);
ChangeWatcher.watch(ArcGISTiledMapServiceLayer(liveLayers), "visibleLayers", updateLegend2);
}
}
}
}