May I know what's wrong with my code below?
I try to send some argument from login screen to home screen, but all I get is error : Error: Property 'settings' cannot be accessed on 'ModalRoute<Object?>?' because it is potentially null.
So my code design is, from my main.dart is calling route_generator which saving all the route, main.dart :
import 'package:flutter/material.dart';
import 'package:navigation/constants/app_color.dart';
import 'package:navigation/route_generator.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My Apps',
theme: ThemeData(
appBarTheme: AppBarTheme(
color: Constant.buttonPrimaryColor,
)),
initialRoute: '/loginScreen',
onGenerateRoute: RouteGenerator.generateRoute,
));
}
route_generator.dart that maintains all the route :
import 'package:flutter/material.dart';
import 'package:navigation/screens/login_screen.dart';
import 'package:navigation/screens/home_screen.dart';
import 'package:navigation/screens/about_screen.dart';
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/loginScreen':
return MaterialPageRoute (builder: (context) => LoginScreen());
case '/homeScreen':
return MaterialPageRoute (builder: (context) => HomeScreen());
case '/aboutScreen':
return MaterialPageRoute (builder: (context) => AboutScreen());
default:
return _errorRoute();
}
}
static Route<dynamic> _errorRoute() {
return MaterialPageRoute(builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text('ERROR'),
centerTitle: true,
),
body: Center(
child: Text('Page not found!'),
),
);
});
}
}
Then some services.dart that navigate based on API response (sent arguments here), so basically i try to redirect to Home Screen from this Login Screen, and from this login screen i try to sent some arguments into Home Screen and try to receive it and show it in the screen, put some dummy data here with name and email :
if (response.statusCode == 201) {
print('Response status code : ' + response.statusCode.toString());
print('response body : ' + response.body);
Navigator.pushNamed(context, '/homeScreen', arguments: {
'name':'Johny',
'email':'johny@johny.com',
});
} else {
print('Response status code : ' + response.statusCode.toString());
print('exception is : ' + response.body);
throw Exception('Failed to login.');
}
This is the home_screen.dart that throws an error from the flutter :
import 'package:flutter/material.dart';
import 'package:navigation/constants/app_color.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
final Map<String, dynamic> map = ModalRoute.of(context).settings.arguments;
return WillPopScope(
onWillPop: () => Future.value(false),
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Home"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Welcome to Apps",
style: TextStyle(
fontSize: 20,
),
),
Text(
"Hi, xxx",
style: TextStyle(
fontSize: 20,
),
),
SizedBox(
height: 10,
),
Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/aboutScreen');
},
child: Text(
"About",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Constant.buttonPrimaryColor),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: BorderSide(color: Constant.buttonPrimaryColor)
)
)
),
),
),
Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
"Logout",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Constant.buttonPrimaryColor),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: BorderSide(color: Constant.buttonPrimaryColor)
)
)
),
),
),
],
),
),
);
}
}
I try to follow some of the tutorials out there, and in the RouteGenerator I already pass the RouteSettings, try to read a couple of times but still confuse, can somebody explain and show me how to pass and show the arguments?
Full log :
lib/screens/home_screen.dart:14:59: Error: Property 'settings' cannot be accessed on
'ModalRoute<Object?>?' because it is potentially null.
- 'ModalRoute' is from 'package:flutter/src/widgets/routes.dart' ('../flutter/packages/flutter/lib/src/widgets/routes.dart').
- 'Object' is from 'dart:core'.
Try accessing using ?. instead.
final Map<String, dynamic> map = ModalRoute.of(context).settings.arguments;
^^^^^^^^
lib/screens/home_screen.dart:14:68: Error: A value of type 'Object?' can't be assigned to a variable of type 'Map<String, dynamic>'.