list - 如何将未来列表设置为列表并更新 ListView
问题描述
每当我单击按钮时,我都需要更新列表视图,我尝试了以下代码,但它不能完美运行,当我单击第一个按钮时它不会更新任何内容,当我单击第二个按钮时它会更新列表使用第一个按钮列表,当我单击第三个按钮列表时,它会使用第二个按钮列表更新列表。这是我的代码
import 'package:flutter/material.dart';
import 'products_list.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<ProductsList> myinitlist = [];
@override
void initState() {
super.initState();
myinitlist = List.from(productList);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
child: SearchBar(),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(4.0, 4.0, 10.0, 4.0),
child: Hero(
tag: 'logo',
child: Image.asset(
'images/iglo_round.png',
height: 30.0,
width: 30.0,
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
children: <Widget>[
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
//i need to update this list with new items
myinitlist.removeRange(0, myinitlist.length);
getAllProductsInCategory(25504207);
myinitlist = List.from(productList);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
//i need to update this list with new items
myinitlist.removeRange(0, myinitlist.length);
getAllProductsInCategory(25504204);
myinitlist = List.from(productList);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
//i need to update this list with new items
myinitlist.removeRange(0, myinitlist.length);
getAllProductsInCategory(25504208);
print(myinitlist.length);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
],
),
),
Flexible(
child: GridView.builder(itemCount: myinitlist.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount
(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index){
return Card(
child: Stack(
children: <Widget>[
GestureDetector(
onTap:(){
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Center(
child: Image.network(myinitlist[index].proThummbnail,
fit: BoxFit.contain,
),
),
flex: 2,
),
Expanded(
child: Center(child: Text(myinitlist[index].proName,
style:
TextStyle(fontSize: 10.0),)),
flex: 1,
)
],
),
)
],
),
);
}),
),
],
),
),
),
);
}
}
products_list.dart 文件包含以下代码
List<ProductsList> productList = [];
class ProductsList{
int proId;
String proName;
String proThummbnail;
ProductsList({@required this.proId,@required this.proName,@required this.proThummbnail});
}
Future<List<ProductsList>> getAllProductsInCategory(int catid) async{
// print(categoryList[0].catId);
String prodInCategoryUrl = 'https://app.ecwid'
'.com/api/v3/12424132/products?token'
'=public_xxxxxxxxxxxx&enabled=true&category=$catid';
Response allProductsInCategory = await get(prodInCategoryUrl);
print(allProductsInCategory.statusCode);
productList.removeRange(0, productList.length);
if(allProductsInCategory.statusCode == 200)
{
var allProductsInCategoryData = allProductsInCategory.body;
int totalcount = jsonDecode(allProductsInCategoryData)['count'];
for(int i=0;i<totalcount;i++)
{
// print(jsonDecode(categoryData)['items'][i]['id']);
// print(jsonDecode(categoryData)['items'][i]['name']);
productList.add(
ProductsList(proId: jsonDecode(allProductsInCategoryData)['items'][i]['id'],
proName: jsonDecode(allProductsInCategoryData)
['items'][i]['name'], proThummbnail: jsonDecode
(allProductsInCategoryData)
['items'][i]['thumbnailUrl']
)
);
}
print('products list length ${productList.length}');
return productList;
}
return productList;;
}
谁能帮我这个。先感谢您。
解决方案
你getAllProductsInCategory
就是未来,所以你需要等待你的未来解决。尝试await
在它前面添加。请注意,您可以使用await
insideasync
方法
推荐阅读
- operating-system - 无论日志中的值更改为什么,我都需要一个 grok 模式来仅保留红色概述的信息
- java - 假装客户端多部分/表单数据 POST 请求给出 400 错误请求
- sql - Big Query 中是否有允许我从起点和终点减去纬度和经度的函数?
- discord - 如何从我的网站发送消息到不和谐?
- google-cloud-platform - 如果变量(地图)中包含指定的字符串,Terraform 需要跳过/忽略资源创建
- android - 将图像从 android 上传到 PHP 服务器 - 如何在“net.gotev:uploadservice:4.6.0”中使用 MultipartUploadRequest - 我有一个 version2.1 的代码
- excel - 过滤 sheet1 上 table1 中的数据并将特定列复制到 sheet2 上的 table2
- c# - .NET 中的反射
- flutter - 如何制作像速卖通一样的类别屏幕?
- flutter-getx - 如何使 getx 颤动以移动到下一个中间件