flutter/dart 将带有 onGenerate 路由的 List<String> 传递到另一个屏幕时遇到错误
flutter/dart errors encountered passing a List<String> with onGenerate route to another screen
我是 flutter 的新手,我正在尝试使用 onGenerate 路由将列表传递到另一个屏幕
我收到一个错误
The argument type 'Object?' can't be assigned to the parameter type 'List<String>'.
我也试过这里的答案
所以我将受影响的路线代码更改为:
case eightyTenAddLot:
final value = settings.arguments as List<String>;
return MaterialPageRoute(builder: (context) => EightyTenAddLot(
addLotData: value)
);
很遗憾,我遇到了一个新错误
The following _CastError was thrown while handling a gesture:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<String>' in type cast
我运行没主意了。也许有人可以伸出援手或提供有用的想法,非常感谢。
这是我的代码:
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,
)
]
),
),
]
),
),
),
),
);
}
}
在我看来你传递的是 Map
而不是 List
。检查您传递给 Navigator.pushNamed
的对象。当您将 snapshot
作为参数传递时,通常会发生这种情况,您可能不知道传递的是什么类型。
我是 flutter 的新手,我正在尝试使用 onGenerate 路由将列表传递到另一个屏幕
我收到一个错误
The argument type 'Object?' can't be assigned to the parameter type 'List<String>'.
我也试过这里的答案
所以我将受影响的路线代码更改为:
case eightyTenAddLot:
final value = settings.arguments as List<String>;
return MaterialPageRoute(builder: (context) => EightyTenAddLot(
addLotData: value)
);
很遗憾,我遇到了一个新错误
The following _CastError was thrown while handling a gesture:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<String>' in type cast
我运行没主意了。也许有人可以伸出援手或提供有用的想法,非常感谢。
这是我的代码:
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,
)
]
),
),
]
),
),
),
),
);
}
}
在我看来你传递的是 Map
而不是 List
。检查您传递给 Navigator.pushNamed
的对象。当您将 snapshot
作为参数传递时,通常会发生这种情况,您可能不知道传递的是什么类型。