有连通性时如何使将来的小部件自动加载数据

时间:2019-10-25 06:41:24

标签: flutter future flutter-layout

我正在尝试制作产品信息应用,我使用将来的构建器来构建类别列表。 我面临两个问题 1.当应用最初脱机然后又变为在线时,它不会加载数据。 2.有什么方法可以获取快照的长度。 我将粘贴以下代码:


FutureBuilder(
                        future: getAllCategory(),
                        builder: (BuildContext context, AsyncSnapshot snapshot) {
                          if (snapshot.data == null) {
                            return Center(
                              child: Container(
                                child: CircularProgressIndicator(),
                              ),
                            );
                          } else {
                            return ListView.builder(
                                scrollDirection: Axis.horizontal,
                                itemCount: categorylist.length != 0 || categorylist.length!= null ?  categorylist.length:0,

                                shrinkWrap: true,
                                itemBuilder: (context, index) {
                                  return GestureDetector(
                                    onTap: () async {
                                      pr.show();

                                      print('clicked custom category');
                                      print(categorylist[index].catName);
                                      print(categorylist[index].catId);

                                      await getAllProductsInCategory(categorylist[index].catId);


                                      setState(() {
                                        catId = categorylist[index].catId;
                                        myinitlist.clear();
                                        myinitlist = List.from(productList);
                                        pr.hide();
                                        _selectedIndex = index;

                                      });





                                    },
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.center,
                                      children: <Widget>[
                                        Card(
                                          color: _selectedIndex != null && _selectedIndex == index
                                              ? selectedColor
                                              : defColor,
                                          elevation: _selectedIndex != null && _selectedIndex == index
                                              ? 4.0
                                              : 1.0,
                                          child: Center(
                                            child: Padding(
                                              padding: const EdgeInsets.all(3.0),
                                              child: Container(

                                                child: Padding(
                                                  padding: const EdgeInsets.all(4.0),
                                                  child: Column(
                                                    children: <Widget>[
                                                      Image.network(
                                                        categorylist[index].catThumbnail,
                                                        width: 80.0,
                                                        height: 50.0,
                                                        scale: 1.0,
                                                      ),
                                                      Padding(
                                                        padding: const EdgeInsets.only(top: 4.0),
                                                        child: Text(
                                                          categorylist[index].catName,
                                                          textAlign: TextAlign.center,
                                                          style: TextStyle(color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w500),
                                                        ),
                                                      )
                                                    ],
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  );
                                });
                          }
                        },
                      ),

我认为,如果有任何方法可以获取快照的长度,那么我可以解决我的第一个问题。

1 个答案:

答案 0 :(得分:0)

我这样更改了代码

def get_func(func_names):
    """Return a list of functions corresponding to a list of names."""

    def square(x):
        return x * x

    def circle(x, y):
        return x * y

    def rectangle(x, y):
        pass

    def triangle(x, y):
        pass

    func_by_name = {
        'square': square,
        'circle': circle,
        'rectangle': rectangle,
        'triangle': triangle,
    }

    return [func_by_name[name] for name in func_names]