при попытки передачи на данных на второй экран (SecondHome) вылетает ошибка "Expected a value of type "User", but got one of type ''Null'", вроде как передал значения в user и в самом SecondHome и в onGenerateRoute, но всёровно вылетает ошибка.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: FirstHome(),
onGenerateRoute: (settings) {
if (settings.name == '/second') {
final user = settings.arguments as User;
return MaterialPageRoute(builder: (context) => SecondHome(user: user));
}
return null;
},
));
}
class FirstHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Home'),
centerTitle: true,
),
body: Center(
child: ElevatedButton(
onPressed: () {
User user = User(name: "Aboba", age: 33);
Navigator.pushNamed(context, "/second", arguments: user);
},
child: Text('Second Home'),
),
),
);
}
}
class SecondHome extends StatelessWidget {
late final User user;
SecondHome({required this.user});
@override
Widget build(BuildContext context) {
RouteSettings settings = ModalRoute.of(context)!.settings;
user = settings.arguments as User;
return Scaffold(
appBar: AppBar(
title: Text('${this.user.name} - ${this.user.age}'),
centerTitle: true,
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go Back'),
),
),
);
}
}