我想将 2 个参数从 'getCodePage' 传递到 'MainPage' 然后我有这个错误。 这是我的 getCodePage 我传递参数的部分:
onTap: (){
final String NumID = ModalRoute.of(context).settings.arguments.toString();
final SmsCode = _controller.text;
final IDNum = NumID.toString();
final url = 'https://***********/api/User/$IDNum?sms=$SmsCode';
Future fetchSMS() async {
final response = await http.get(url);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception('Failed');
}
}
fetchSMS().then((resaultOfFeture) {
final args = {
'rof' : resaultOfFeture,
'idNum' : IDNum,
};
if(resaultOfFeture == true){
final snackBar = SnackBar(content: Text('با موفقیت وارد شدید',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: "IranSans",
fontSize: 18
),
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => MainPage(),
settings: RouteSettings(
arguments: args
),
));
}else{
final snackBar1 = SnackBar(content: Text('کد اشتباه وارد شده',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: "IranSans",
fontSize: 18
),
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar1);
}
});
},
当我想将参数作为对象时,这是我的 mainPage:
final logedUser = ModalRoute.of(context).settings.arguments;
final data = logedUser['rof'];
然后我在这里有这个错误: 运算符 '[]' 未为类型 'Object' 颤振错误定义
答案 0 :(得分:0)
尝试初始化您在 MainPage
中传递给 didChangeDependencies
的参数
类似的东西
MainPage.dart
class MainPage extends StatefulWidget {
static const route = "/main-page";
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
var args;
var data;
@override
void didChangeDependencies() async {
if (args != ModalRoute.of(context).settings.arguments) {
args = ModalRoute.of(context).settings.arguments;
data = args["rof"];
}
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
...