我想在我们发布新的数据后获得数据。现在我需要重新打开应用程序来获取插入的数据。如图,我在下面展示。在 主页。 从响应JSON中获得的详细信息。主页 扣子 添加数据页. 当我点击按钮进入 添加数据页 然后在添加数据,这是 电子邮件、用户名 我使用Navigator.of(context).pop();回到我的主页。问题是数据没有更新,我需要重新打开应用才能看到新插入的数据。
这是我的主页代码。
Future getDetails() async {
http.Response response = await http.get(url);
var decodedData = json.decode(response.body);
setState(() {
email= decodedData['email'];
username= decodedData['username'];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home page'),
),
body: SingleChildScrollView(
child: Column(
children: <Widget> [
new Row(
children: [
Text(' Name: '+ username, style: TextStyle(fontSize: 16), textAlign: TextAlign.left,),
Text(' Email : '+ email, style: TextStyle(fontSize: 16), textAlign: TextAlign.left,),
]
),
new Row(
children: [
SizedBox(
width: 350,
child: OutlineButton(
child: Text('ADD DATA'),
onPressed: () {Navigator.of(context).push(MaterialPageRoute(builder: (context) => AddPage()));},
)
)
]
),
]
),
)
);
}
这是我的添加数据页面代码。
Future<void> addDetails() async {
setState(() {
loading = true;
});
http.Response response = await http.post(url,
body: {
'username': usernameController.text,
'email': emailController.text,
}
);
setState(() {
loading = true;
Navigator.pop(context);
});
}
有谁知道如何解决这个问题吗?谢谢大家了。
解决方案:
首先你要改变你的 导航仪#推送 到 await
结果 导航仪#pop 方法。导航仪#pop 有一个带有另一个参数的签名,它将在主页上被接受。你可以在你的主页上试试这样的东西。
new Row(
children: [
SizedBox(
width: 350,
child: OutlineButton(
child: Text('ADD DATA'),
onPressed: () async {
final List<String> pageResult = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => AddPage()));
setState(() {
email = pageResult[0];
userName = pageResult[1];
});
},
)
)
]
);
而在你的其他页面的pop部分做这个。
setState(() {
loading = false; //This is a bug in your code
Navigator.pop(context, [emailController.text, userNameController.text]);
});