Navigator.pushNamed(context, '/mortgage_screen', arguments: {'mortgageModel': mortgageModel});
Should be:
Navigator.pushNamed(context, '/mortgage_screen', arguments: mortgageModel),
Your code above is passing a Map<String,MortgageModel>
type object, not a MortgageModel
type object.
And I'm guessing this was a typo: !
(excalamation point) in your args assignment statement? I can't think of a reason why it would be there.
ModalRoute.of(context)!.settings.arguments;
Here's a complete example:
import 'package:flutter/material.dart';
class PassArgsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
'/': (context) => PageOne(),
PageTwo.routeName: (context) => PageTwo()
},
);
}
}
class MortgageModel {
int yearsLeft = 30;
}
class PageOne extends StatelessWidget {
@override
Widget build(BuildContext context) {
MortgageModel mortgageModel = MortgageModel();
return Center(
child: RaisedButton(
child: Text('Go 2 /w Args'),
onPressed: () => Navigator.pushNamed(context, PageTwo.routeName, arguments: mortgageModel),
),
);
}
}
class PageTwo extends StatelessWidget {
static const routeName = '/pageTwo';
@override
Widget build(BuildContext context) {
MortgageModel mortgageModel = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(title: Text('Pass Args P.2'),),
body: Center(
child: Text('Mortgage years remaining: ${mortgageModel.yearsLeft}'),
),
);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…