嗨,我是经验丰富的离子开发人员,但我是新的扑动我试图在扑动屏幕之间传递数据,但当它导航时它显示空值,在导航之前的调试模式下有导航后显示空值,这里是我的代码,请纠正我的代码谢谢。
屏幕1
import 'package:flutter/material.dart';
import 'package:date_field/date_field.dart';
import 'package:headletter/screens/Add_horoscope/marriage_info.dart';
class AddBasicInfo extends StatefulWidget {
const AddBasicInfo({Key? key}) : super(key: key);
@override
_AddBasicInfoState createState() => _AddBasicInfoState();
}
class _AddBasicInfoState extends State<AddBasicInfo>{
@override
void initState() {
super.initState();
}
Widget build(BuildContext context) {
return MaterialApp()....
bottomNavigationBar: BottomAppBar(
child: GestureDetector(
onTap: (){
final data1 = [
{
"Name": inputName.text,
"Gender": _selectedGender,
"DOB":dobDate,
"DobTime":dobTime,
"Place": birthPlace.text,
"Landmark": landmark.text,
"birthNo": _selectedChild
}
];
final String passingValue = "checking for passing";
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
MarriageInfo(passingValue: passingValue)),
);
}
}
},
2屏幕
class MarriageInfo extends StatefulWidget {
final String passingValue;
const MarriageInfo({Key? key, required this.passingValue}) : super(key: key);
@override
_MarriageInfoState createState() => _MarriageInfoState();
}
到目前为止,这是正确的,但在屏幕2上使用passingValue
时,您必须使用widget.passingValue
例:print(widget.passingValue);
你正在使用statefulwidget
你可以这样描述statefullwidget的构造函数
const MarriageInfo (this.number);
,然后你可以用
超出这个值widget.passingValue
您应该使用widget。passingValue,而不仅仅是变量名。属性被分配给那个小部件,而不是那个状态类。