Reputation: 87
I have a Future function "_doSearch" which fetch data from server and it is working fine. But when I add a another function inside it to display progress dialog while fetching data from the server the progress dialog is displayed but the Future function stop fetching data!!! Any body can help ?
Future<List<Property>> _doSearch () async{
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context){
return MyProgressDialog("Please wait...");
}
);
Response response = await get("https://www.example.com/services/?action=search");
if(response.statusCode == 200){
Navigator.pop(context);
var jsonData = json.decode(response.body);
var res = jsonData["results"];
List<Property> results = [];
for (var p in res){
Property unit = Property(p["property_title"], p["property_image"]);
results.add(unit);
}
print(results.length);
return results;
}else{
Navigator.pop(context);
}
}
Upvotes: 1
Views: 477
Reputation: 69
You are killing the function using return MyProgressDialog("Please wait...");
Modify the code like this
Future<List<Property>> _doSearch () async{
pr = ProgressDialog(context,type: ProgressDialogType.Normal, isDismissible: false);
pr.style(
message: 'Please wait...',
borderRadius: 10.0,
backgroundColor: Colors.white,
progressWidget: CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation<Color>(Colors.orange),),
elevation: 10.0,
insetAnimCurve: Curves.easeInOut,
progressTextStyle: TextStyle(
color: Colors.black, fontSize: 13.0, fontWeight: FontWeight.w400),
messageTextStyle: TextStyle(
color: Colors.black, fontSize: 19.0, fontWeight: FontWeight.w600)
);
await pr.show();
Response response = await get("https://www.example.com/services/?action=search");
if(response.statusCode == 200){
Navigator.pop(context);
var jsonData = json.decode(response.body);
var res = jsonData["results"];
List<Property> results = [];
for (var p in res){
Property unit = Property(p["property_title"], p["property_image"]);
results.add(unit);
}
print(results.length);
pr.hide();
return results;
}else{
pr = ProgressDialog(context,type: ProgressDialogType.Normal, isDismissible: false);
pr.style(
message: 'Please wait...',
borderRadius: 10.0,
backgroundColor: Colors.white,
progressWidget: CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation<Color>(Colors.orange),),
elevation: 10.0,
insetAnimCurve: Curves.easeInOut,
progressTextStyle: TextStyle(
color: Colors.black, fontSize: 13.0, fontWeight: FontWeight.w400),
messageTextStyle: TextStyle(
color: Colors.black, fontSize: 19.0, fontWeight: FontWeight.w600)
);
await pr.show();
Navigator.pop(context);
}
}
Do remember to add and import progress_dialog
Upvotes: 0
Reputation: 104
Use this method
static progressDialog(bool isLoading, BuildContext context) {
AlertDialog dialog = new AlertDialog(
content: new Container(
height: 40.0,
child: new Center(
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new CircularProgressIndicator(),
Padding(padding: EdgeInsets.only(left: 15.0)),
new Text("Please wait")
],
),
)),
contentPadding: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 15.0),
);
if (!isLoading) {
Navigator.of(context, rootNavigator: true).pop();
} else {
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return WillPopScope(onWillPop: () {}, child: dialog);
},
useRootNavigator: true,
);
}
}
example:
Constants.progressDialog(true, context);
Response response = await get("https://www.example.com/services/?action=search");
Constants.progressDialog(false, context);
if(response.statusCode == 200){
var jsonData = json.decode(response.body);
var res = jsonData["results"];
List<Property> results = [];
for (var p in res){
Property unit = Property(p["property_title"], p["property_image"]);
results.add(unit);
}
print(results.length);
return results;
}
Upvotes: 1