其他分享
首页 > 其他分享> > flutter FutureBuilder应用

flutter FutureBuilder应用

作者:互联网

Widget Invlist(WidgetRef ref) {
  //这是一个异步函数,用于给FutureBuilder组件调用
  Future<List<Invinfo>> getinvlist() async {
    var invlist =await ref.watch(eleinfo_Provider).invlist;
    return invlist;
  }

  //final invlist = ref.watch(eleinfo_Provider).invlist;
  final count = ref.watch(eleinfo_Provider).invlist.length;
  return Column(
    children: [
      ElevatedButton(
          onPressed: () {
            ref.read(eleinfo_Provider).readinvlist();
          },
          child: Text('获取数据')),
      Text('${count}'),
       Container(
         alignment: Alignment.center,
          height: 600,
          child: FutureBuilder(
            future: getinvlist(),// 调用的函数 函数返回的是 List<Invinfo>类型
            builder: (context, snapshot) {
              if (snapshot.hasData && (snapshot.data as List<Invinfo>).length>0) {//用于判断是否有数据
                final templist = (snapshot.data as List<Invinfo>); //函数的数据存放于snapshot.data,为了下面编码方便,存放在一个临时变量里
                return ListView.builder(
                    itemCount: (snapshot.data as List<Invinfo>).length,
                    itemBuilder: (context, index) {
                      return ListTile(
                        leading: Icon(Icons.shop,color: Colors.blue,),
                        title: Text(templist[index].cInvCode),
                        subtitle: Text(templist[index].cInvName),
                      );
                    });
              } else {
                return Text('没有数据');
              }
            },
          ))
    ],
  );
}

 

标签:return,应用,Text,FutureBuilder,snapshot,invlist,Provider,ref,flutter
来源: https://www.cnblogs.com/wuxi15/p/16296827.html