我在第一页上有 2 个页面,我正在从 API 调用数据。在第二页上,我正在编辑数据。但问题是,当编辑完成后,我弹出的第一页数据没有在小部件中更新。所以我必须使用 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 将再次运行,因此它没有运行,这就是它也没有更新状态的原因。我被困在这一点上是不可能刷新状态还是什么? -_-
尝试这样的事情:
Navigator.push(context, secondScreen).then((result) => setState(() {}));
当您从一个屏幕导航到另一个屏幕时,可以向
pop
添加回调并刷新上一个屏幕。
要添加到fartem的答案中,您可能还需要调用用于加载数据的函数
Navigator.push(context, secondaryScreen).then((结果) => setState(() { 获取地址(); }));