dart - 在从 api 填充列表时显示加载指示器

标签 dart flutter

我是 Flutter 的新手……我在我的应用程序中调用一个 API,我想在 API 函数完成时显示一个加载指示器,并且从 API 中检索所有内容……如何实现这一点?

class _CompaniesPageState extends State<CompaniesPage> {
  getcompanies() async {
  var url = 'my link';
  http
    .post(url, body: json.encode({ 'token': globals.token }))
    .then((response) {
      // print("Response body: ${response.body}");
      Map data = json.decode(response.body);
      final companies =
        (data['Companies'] as List).map((i) => new Company.fromJson(i));
      setState(() {
        for (final company in companies) {
          if (!company.category.contains("الراعي") &&
            !company.category.contains("الشريك") &&
            !company.category.contains("الداعم")) {
            if (company.logo != "") {
              names.add(company.name);
              logos.add(company.logo);
            }
          }
        }
      });
    });
}

@override
Widget build(BuildContext context) {
  getcompanies();
  if (globals.isguest) {
    return new Directionality(
      .....
    );
  }

}

List<Widget> createCompaniesChildren() {
  List<Widget> children = List<Widget>();
  for (int i = 0; i < names.length; i++) {
    children.add(
      new Padding(
        padding: new EdgeInsets.only(right: 15.0, left: 15.0),
        child: new Image.network(
          logos[i],
          width: 346.0,
          height: 180.0,
        ),
      ),
    );
    children.add(
      new Padding(
        padding: new EdgeInsets.only(right: 15.0, left: 15.0),
        child: new Center(
          child: new Text(
            names[i],
            style: new TextStyle(color: Colors.black, fontSize: 17.0),
            textAlign: TextAlign.center,
          ),
        ),
      ),
    );
    children.add(new Divider());
  }
  return children;
}

如何在列表正在填充时显示加载指示器,然后在完成后将其关闭?

试过这个:

          body: new Padding(
        padding: new EdgeInsets.only(top: 15.0),
        child: new Center(
          child: new FutureBuilder(
            future:
                getcompanies(), // your async method that returns a future
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                // if data is loaded
                if (snapshot.data != null) {
                  return new ListView.builder(
                    padding: new EdgeInsets.all(8.0),
                    itemExtent: 20.0,
                    itemBuilder: (BuildContext context, int i) {
                      return new Center(
                        child: new Text(
                          snapshot.data[i].name,
                          style: new TextStyle(
                              color: Colors.black, fontSize: 17.0),
                          textAlign: TextAlign.center,
                        ),
                      );
                    },
                  );
                } else {
                  // if data not loaded yet
                  return new CircularProgressIndicator();
                }
              }
            },
          ),
        ),
      ),

但是出现了这个错误:I/flutter (31276): Another exception was throwed: A build function returned null.

最佳答案

您可以使用 FutureBuilder为此目的上课。

此外,您可以使用 ListView 而不是手动创建公司列表。 .

像这样:

new FutureBuilder(
  future: getcompanies(), // your async method that returns a future
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    if (snapshot.hasData) {
      // if data is loaded
      return new ListView.builder(
        padding: new EdgeInsets.all(8.0),
        itemExtent: 20.0,
        itemCount: snapshot.data.length,
        itemBuilder: (BuildContext context, int i) {
          return new Center(
            child: new Text(
              snapshot.data[i].name,
              style: new TextStyle(color: Colors.black, fontSize: 17.0),
              textAlign: TextAlign.center,
            ),
          );
        },
      ).build(context);
    } else {
      // if data not loaded yet
      return new CircularProgressIndicator();
    }
  },
)

关于dart - 在从 api 填充列表时显示加载指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52754828/

相关文章:

java - 如果使用 Android 后台服务,Flutter 卡在 'waiting for observatory port to be available'

dart - 将字符串列表转换为int Dart列表

flutter - Flutter:使用自定义ErrorWidget

listview - Flutter:ListView.builder 中的 ListView.builder

flutter - 如何在Dart函数中使参数和变量的名称不同

flutter - 如何在 flutter 的 ListView 中设置卡片的宽度?

android - 如何在 Flutter 中使 Expanded 中的文本可滚动

android - "flutter (os error: no address associated with hostname, errno = 7)"仅适用于安卓

flutter - 如何使 Bottom Sheet 格的高度跟随 JSON 的项目数量

git - VS Code 中文件名旁边的 "A "是什么意思?