Reputation: 359
I have 2 pages on the first page I am calling data from an API. And on the second page, I am editing the data. But the issue is when the edit is done and I am pop bak to the first-page data is not updating in the widget. So I have to use the Nav.pop(context, true) method but it's not working.
My first page code
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(),
],
),
),
);
}
}
On the second page, I am coming back like this Navigator.pop(context,true);
But the page state isn't changing or refresh. What I need to is when I come back I can see the edit changes so it means my API will run again so it is not running that's why it is not updating the state also. I am stuck at this point is it not possible to refresh state or what? -_-
Upvotes: 1
Views: 1020
Reputation: 11
To add on to fartem's answer, you might also have to call the function with which you load the data with
Navigator.push(context, secondScreen).then((result) => setState(() { getAddress(); }));
Upvotes: 0
Reputation: 2541
Try something like this:
Navigator.push(context, secondScreen).then((result) => setState(() {}));
When you navigate from one screen to another, it is possible add callback to pop
and refresh previous screen.
Upvotes: 1