Reputation: 12754
I'm new to flutter and I'm trying to pass a List to another screen using onGenerate routes
I'm getting an error
The argument type 'Object?' can't be assigned to the parameter type 'List<String>'.
I also tried the answer here How to pass value through constructor to another screen using generating route method with flutter?
So I changed my affected route code into:
case eightyTenAddLot:
final value = settings.arguments as List<String>;
return MaterialPageRoute(builder: (context) => EightyTenAddLot(
addLotData: value)
);
Unfortunately I'm getting a new error
The following _CastError was thrown while handling a gesture:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<String>' in type cast
I ran out of ideas. Maybe someone can give a hand or helpful ideas are very much appreciated.
Here are my codes:
route.dart
// Route Names
const String loginPage = 'loginPage';
const String dashBoard = 'dashboard';
const String eightyTenTabletPg2 = 'eightyTenTabletPg2';
const String eightyTenAddLot = 'eightyTenAddLot';
// Control our page route flow
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case loginPage:
return MaterialPageRoute(builder: (context) => LoginPage());
case dashBoard:
return MaterialPageRoute(builder: (context) => Dashboard());
case eightyTenTabletPg2:
return MaterialPageRoute(builder: (context) => EightyTenTabletPg2(
recvNo: settings.arguments.toString()
));
case eightyTenAddLot:
print('settings.arguments: ${settings.arguments}');
return MaterialPageRoute(builder: (context) => EightyTenAddLot(addLotData: settings.arguments,));
default:
throw('The route does not exist yet.');
}
}
eighty_ten_add_lot.dart
class EightyTenAddLot extends StatefulWidget {
final List<String> addLotData;
const EightyTenAddLot({Key? key, required this.addLotData}) : super(key: key);
@override
_EightyTenAddLotState createState() => _EightyTenAddLotState();
}
class _EightyTenAddLotState extends State<EightyTenAddLot> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
title: const Text('8010 - Add Lot'),
leading: IconButton(icon: const Icon(Icons.arrow_back),
onPressed:() => Navigator.pop(context, false),
)
),
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
// mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 300,
child: Container(
decoration: const BoxDecoration(
color: Color(0xFF3F51B5),
borderRadius: BorderRadius.all(Radius.circular(5)),
),
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: Text("Add Lot",
style: TextStyle(
fontSize: 22.0,
color: Colors.white,
),
),
),
),
),
),
const SizedBox(
height: 20.0,
),
//main content
Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
width: MediaQuery.of(context).size.width * 0.5,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(15)),
boxShadow: [
BoxShadow(
blurRadius: 10, color: Colors.grey.shade300, spreadRadius: 5)
]),
child: Column(
children: [
const SizedBox(
height: 2.0,
),
_createLabelInput('Managed Date'),
const SizedBox(
height: 2.0,
),
_createLabelInput('Expiration Date'),
const SizedBox(
height: 2.0,
),
_createLabelInput('LOT'),
const SizedBox(
height: 2.0,
),
_createLabelInput('Inspected Qty.'),
// Submit button
SizedBox(
height: 50,
child: ElevatedButton(
child: const Text(
'Submit',
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
print('addLotMap: $addLotMap');
//_eightyTen_40W(mngDateController.text, expiryDateController.text, lotController.text, inspectedQtyController.text);
},
),
),
const SizedBox(
height: 10.0,
)
]
),
),
]
),
),
),
),
);
}
}
Upvotes: 0
Views: 386
Reputation: 96
It seems to me you passing Map
instead of List
. Check object you passing to the Navigator.pushNamed
. It usually happens when you pass a snapshot
as an argument you may not know what type you passing.
Upvotes: 1