database - 将 API 数据从第 1 页传递到 Flutter 上的另一个 dart 页面
问题描述
已经有来自 API 的数据的页面,然后我想将 API 数据发送到下一页。
例如:当我们点击食物列表时,会出现点击的食物的描述,或者当我们点击另一个页面的数据时,它比列表上的数据更详细
以下是起始页的代码:
var notifikasiSiswa = [];
//====================== Fungsi ListView ================================
class _NotifikasiSiswaState extends State<NotifikasiSiswa> {
Future<SharedPreferences> _sprefs = SharedPreferences.getInstance();
int id;
@override
Future<Null> getData() async {
final SharedPreferences prefs = await _sprefs;
int data = prefs.getInt('id');
this.setState(() {
id = data ;
});
// counter = data;
// api coba dsni
NotifikasiSiswaMo.getPengumuman(id.toString()).then((value) {
notifikasiSiswa = value;
this.setState(() {
notifikasiSiswa = value;
});
print("muncul");
});
// print(token);
}
@override
void initState() {
super.initState();
setState(() {
getData();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Materi Siswa"),),
body: ListView.builder( // Auto Scroll Jika Data yang dimunculkan banyak
reverse: true,
itemCount: notifikasiSiswa.length,
itemBuilder: (context, index){
return Center(
child: Padding(
padding: EdgeInsets.only(left: 20.0, right: 20.0, top: 15.0 ),
child: Stack(
children: <Widget>[
Container(
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(_borderRadius),
gradient: LinearGradient(
colors: [Colors.white, Colors.white],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
)
),
Positioned.fill(child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return NotifikasiSiswaDetail(
s_date : notifikasiSiswa[index].date,
s_title : notifikasiSiswa[index].title,
s_message :notifikasiSiswa[index].message
);
},
));
},
child: Row(
children: <Widget>[
Expanded(
child:
Center(
child: Icon(Icons.notifications_active))
),
Expanded(
flex: 4,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, //Vertical dengan posisi aligmn start
children: <Widget>[
Text(notifikasiSiswa[index].date ?? "00/00/00",
style: TextStyle(
color: Colors.black45,
fontWeight: FontWeight.w500),),
SizedBox(
height: 6.0,
),
如何在下一页?
解决方案
final List args = ModalRoute.of(context).settings.arguments;如果我正确理解了您的问题,您可以arguments
使用Navigator.pushNamed()
.
Navigator.pushNamed(context, '/newPage', arguments: /* data you want to pass */);
要从新页面访问此数据,您可以在其build
方法中使用此行。
final args = ModalRoute.of(context).settings.arguments;
您可以在此处了解更多信息:https ://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
推荐阅读
- java - 错误的 WebTarget 实现 SseEventSource
- php - Rails 中的脏方法等效于 PHP Codeignitor
- php - 从 Laravel factory() 获得意外响应导致分段错误(核心转储)
- python - 如何在文本中查找字符串并从文本中返回字符串?
- java - 如何将 CSV 文件数据存储到 Java 中的数组中?
- ios - 如何在 Swift 的 1 个字节上编码扩展的 ASCII/macOS Roman(字符从 128 到 255)?
- python - 根据特定键合并2个字典列表
- vb.net - 如何在 ESC/POS 打印机上打印阿拉伯语
- javascript - 在 a 中添加一个属性如果属性有值
- kubernetes - 如何将 yaml configmap 文件转换为 terraform