json - Flutter - 在使用 Future 加载本地 JSON 时设置加载动画
问题描述
我正在尝试在加载本地 JSON 文件时实现加载动画逻辑(可能是循环进度指示器)。我正在为此使用颤振的未来,但我找不到合适的方法。
我有一个对象列表,[id: int, country: String, state: String]
我在卡片中一次显示一个对象,最终结果应该是第一次循环进度
到目前为止,这是我的代码:
class _BottomCardsState extends State<BottomCards>{
Future<Null> getAllData() async{
var response = await DefaultAssetBundle.of(context).loadString('assets/json/data.json');
var decodedData = json.decode(response);
setState(() {
for(Map element in decodedData){
mDataList.add(Country.fromJson(element));
}
});
}
@override
void initState() {
super.initState();
getAllData();
}
int index = mDataList.first.id;
@override
Widget build(BuildContext context) {
int n = mDataList[index].id;
return Container(
child: Material(
color: Colors.transparent,
child: Card(
margin: EdgeInsets.symmetric(horizontal: 10.0),
elevation: 6.0,
child: Container(
alignment: Alignment.topCenter,
padding: EdgeInsets.symmetric(horizontal: 12.0,),
child: Column(
Row(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Column(
children: <Widget>[
Text(mDataList[index].country + ' ' + mDataList[index].state),
],
),
],
),
SizedBox(height: 8.0,),
Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Chip(
backgroundColor: secondaryDark,
label: Text('Info number: $n'),
),
],
),
SizedBox(height: 6.0),
],
),
),
),
),
);
}
}
解决方案
这是我的解决方案
class _BottomCardsState extends State<BottomCards>{
bool isLoadingEnded=false;
Future<Null> getAllData() async{
await DefaultAssetBundle.of(context).loadString('assets/json/data.json').then((response){
var decodedData = json.decode(response);
isLoadingEnded=true;
setState(() {
for(Map element in decodedData){
mDataList.add(Country.fromJson(element));
}
});
})
}
@override
void initState() {
super.initState();
getAllData();
}
int index = mDataList.first.id;
@override
Widget build(BuildContext context) {
int n = mDataList[index].id;
return isLoadingEnded ? Container(
child: Material(
color: Colors.transparent,
child: Card(
margin: EdgeInsets.symmetric(horizontal: 10.0),
elevation: 6.0,
child: Container(
alignment: Alignment.topCenter,
padding: EdgeInsets.symmetric(horizontal: 12.0,),
child: Column(
Row(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Column(
children: <Widget>[
Text(mDataList[index].country + ' ' + mDataList[index].state),
],
),
],
),
SizedBox(height: 8.0,),
Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Chip(
backgroundColor: secondaryDark,
label: Text('Info number: $n'),
),
],
),
SizedBox(height: 6.0),
],
),
),
),
),
) : Center(child:new CircularProgressIndicator());
}
}
推荐阅读
- button - 单击按钮时隐藏表单域
- google-chrome - Firefox 阻止 CORS 而 Chrome 允许,使用 Nginx
- html5-canvas - 使用“ImageData”数据数组制作动画会导致非常意外和类似故障的结果
- javascript - 如何在不使用状态的情况下在 React Native 中获取 TextInput 的值
- php - CodeIgniter 有时会将我重定向到主页
- c# - c# - 运行多组并行线程 (Parallel.ForEach)
- c - 使用堆栈计算表达式 (C)
- c# - 从 .Large json 文件创建 Excel
- java-8 - 将“传递引用”传递给与抽象方法的参数列表匹配的方法时,java 8 如何在内部进行评估?
- search - 浏览 Uipath 中的目录