显示进度对话框停止未来功能

时间:2020-10-23 08:12:45

标签: flutter

我有一个Future函数“ _doSearch”,该函数从服务器获取数据,并且工作正常。 但是,当我在其中添加另一个函数以显示进度对话框时,从服务器中获取数据时,将显示进度对话框,但Future函数将停止获取数据! 任何人都能帮忙吗?

The following assertion was thrown during performLayout():
FixedExtentScrollPhysics can only be used with Scrollables that uses the FixedExtentScrollController
'package:flutter/src/widgets/list_wheel_scroll_view.dart':
Failed assertion: line 485 pos 7: 'position is _FixedExtentScrollPosition'

2 个答案:

答案 0 :(得分:1)

使用此方法

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,
      );
    }
  }

示例:

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;
    }

答案 1 :(得分:0)

您正在使用return MyProgressDialog(“ Please wait ...”);

像这样修改代码

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);
    }
  }

请记住要添加和导入 progress_dialog