Flutter页面状态在返回时不刷新
Posted
技术标签:
【中文标题】Flutter页面状态在返回时不刷新【英文标题】:Flutter page state isnt refreshing when coming back 【发布时间】:2021-04-30 01:58:30 【问题描述】:我从 API 调用数据的第一页上有 2 页。在第二页上,我正在编辑数据。但问题是当编辑完成并且我弹出到第一页数据没有在小部件中更新时。所以我必须使用 Nav.pop(context, true) 方法但它不起作用。
我的第一页代码
class _AddressPageState extends State<AddressPage>
var address = 'Address': [];
bool showAddress = false;
int dateindex = 0;
var addressSelected;
@override
void initState()
setState(()
getAddress();
);
getAddress() async
final storage = new FlutterSecureStorage();
String _userEmail = await storage.read(key: "_userEmail");
String _userPassword = await storage.read(key: "_userPassword");
String url =
'http://retailapi.airtechsolutions.pk/api/customer/login/$_userEmail/$_userPassword';
print(url);
http.Response res = await http.get(
url,
);
var data = json.decode(res.body.toString());
print(data);
if (data['description'].toString() == "Success")
print(data['customer']['Addresses']);
address['Address'].addAll(data['customer']['Addresses']);
print(address);
setState(()
showAddress = true;
);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: buildAppBar(context),
body: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
showAddress
? Flexible(
flex: 9,
child: SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 20.0),
Text(
'order.shippingaddress',
style: Theme.of(context).textTheme.headline4,
).tr(),
SizedBox(height: 20.0),
ListView.builder(
itemCount: address['Address'].length,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
itemBuilder: (context, index)
return SideInAnimation(
index,
child: GestureDetector(
// onTap: widget.onPressed,
onTap: ()
setState(()
dateindex = index;
addressSelected =
address['Address'][index];
print(addressSelected);
);
,
child: Container(
width: double.infinity,
padding: EdgeInsets.all(15.0),
margin: EdgeInsets.only(bottom: 15.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
border: Border.all(
color: dateindex == index
? Theme.of(context).primaryColor
: Theme.of(context).accentColor,
width: dateindex == index ? 2.0 : 1.0,
),
),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
showAddress
? address['Address'][index]
['Address']
: '...',
style: Theme.of(context)
.textTheme
.headline4),
SizedBox(height: 8.0),
Text(
showAddress
? address['Address'][index]
['Address']
: '...',
style: Theme.of(context)
.textTheme
.subtitle1),
SizedBox(height: 8.0),
Text(
showAddress
? address['Address'][index]
['Address']
: '...',
style: Theme.of(context)
.textTheme
.subtitle1),
SizedBox(height: 8.0),
Row(
children: [
SizedBox(
width: 80.0,
child: RaisedButtonWidget(
title: 'product.edit',
onPressed: ()
// Get.to(EditAddressPage(address: address['Address'][index]));
Navigator.push(context, MaterialPageRoute(builder: (context) => EditAddressPage(address: address['Address'][index]))).then((value)
if (value == true)
setState(()
);
);
,
),
),
SizedBox(width: 15.0),
IconButton(
icon: Icon(Icons.delete_outline),
onPressed: ()
// showDeleteConfirmation(context);
,
),
],
)
],
),
),
),
);
,
),
SizedBox(height: 25.0),
],
),
),
)
: Container(),
buildConfirmAddressButton(),
],
),
),
);
在第二页,我像这样回来Navigator.pop(context,true);
但是页面状态没有改变或刷新。我需要做的是,当我回来时,我可以看到编辑更改,这意味着我的 API 将再次运行,因此它没有运行,这就是它也没有更新状态的原因。我被困在这一点上是不可能刷新状态还是什么? -_-
【问题讨论】:
您是否考虑将新数据弹出回首页? 【参考方案1】:试试这样的:
Navigator.push(context, secondScreen).then((result) => setState(() ));
当您从一个屏幕导航到另一个屏幕时,可以向pop
添加回调并刷新上一个屏幕。
【讨论】:
以上是关于Flutter页面状态在返回时不刷新的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 状态管理 | StreamBuild 局部刷新的效果Flutter局部刷新