Flutter FutureBuilder异步请求列表示例

Flutter的FutureBuilder列表示例

import 'package:flutter/material.dart';
import '../service/service_method.dart';

class CartPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var formData = {'adCode': '000000', 'isDebug':'aa'};
    return Scaffold(
      body: FutureBuilder( //异步请求再渲染 不用setState改变状态
        future: request('get','chinaAddr',formData:formData), //可选参数formData:formData=属性名:属性值
        builder: (context, snapshot){ //snapshot就是_calculation在时间轴上执行过程的状态快照
           if(snapshot.hasData){ //判断有没有值
              //print(snapshot.data);

              //数据处理
              var data = snapshot.data;
              List<Map> chinaData = (data['data'] as List).cast();
              //print(chinaData);

              return ListView.builder(
                itemCount: chinaData.length,
                itemBuilder: (context, index) {
                  final Map<String, dynamic> item = (chinaData)[index];
                  return ListTile(
                    title:  Text('${item["adName"]}'),
                    subtitle: Text('${item["adCode"]}'),
                  );
                }
              );

           }else{
              return Center(
                child: Text('加载中...'),
              );
           }
         }  
      ),
    );
  }
}

效果图:

 

posted on 2019-09-30 09:12  JoeYoung  阅读(1919)  评论(0编辑  收藏  举报