类型 'String' 不是类型转换中类型 'Map<String, String>' 的子类型
type 'String' is not a subtype of type 'Map<String, String>' in type cast
当我点击我的 Flutter 应用程序中的按钮时出现错误
这里有人有解决方案吗?!
路线
routes:{
'/':(context) => CategoriesScreen(), // the HomePage
CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),}
参数
void selectCategory(BuildContext ctx) {
Navigator.of(ctx).pushNamed(CategoryMealsScreen.routeName,
arguments: {
'id': id,
'title': title,
});
和地图
Widget build(BuildContext context) {
final routeArg = ModalRoute.of(context)?.settings.arguments as Map<String, String>; // the question mark is needed but I don't know why..!
final categoryId = routeArg["id"];
final categoryTitle = routeArg["title"];
final categoryMeals = DUMMY_MEALS.where((meal){
return meal.categories.contains(categoryId);
}).toList();
完整错误
构建 CategoryMealsScreen 时抛出了以下 _CastError(脏,依赖项:[_ModalScopeStatus],状态:_CategoryMealsScreenState#b3c64):
type 'String' 不是 type cast
中 type 'Map<String, String>' 的子类型
导致错误的相关小部件是:
CategoryMealsScreen file:///C:/Users/DELL/AndroidStudioProjects/meal_app/lib/main.dart:37:50
抛出异常时,这是堆栈:
#0 _CategoryMealsScreenState.build(包:meal_app/screens/category_meals_screen.dart:16:65)
#1 StatefulElement.build(包:flutter/src/widgets/framework.dart:4691:27)
#2 ComponentElement.performRebuild(包:flutter/src/widgets/framework.dart:4574:15)
#3 StatefulElement.performRebuild(包:flutter/src/widgets/framework.dart:4746:11)
#4 Element.rebuild(包:flutter/src/widgets/framework.dart:4267:5)
演示
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, routes: {
'/': (context) => CategoriesScreen(), // the HomePage
CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),
});
}
}
class CategoriesScreen extends StatefulWidget {
CategoriesScreen({Key? key}) : super(key: key);
@override
_MyWidgetXState createState() => _MyWidgetXState();
}
class _MyWidgetXState extends State<CategoriesScreen> {
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.of(context)
.pushNamed(CategoryMealsScreen.routeName, arguments: {
'id': "id 1",
'title': "title",
});
},
child: Text("pop"),
),
],
),
);
}
}
class CategoryMealsScreen extends StatefulWidget {
static final String routeName = "/asdasd";
CategoryMealsScreen({Key? key}) : super(key: key);
@override
_MyW2State createState() => _MyW2State();
}
class _MyW2State extends State<CategoryMealsScreen> {
@override
Widget build(BuildContext context) {
final routeArg = ModalRoute.of(context)?.settings.arguments as Map<String,
String>; // the question mark is needed but I don't know why..!
final categoryId = routeArg["id"];
final categoryTitle = routeArg["title"];
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("pop, $categoryTitle $categoryId"),
),
),
);
}
}
当我点击我的 Flutter 应用程序中的按钮时出现错误 这里有人有解决方案吗?!
路线
routes:{
'/':(context) => CategoriesScreen(), // the HomePage
CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),}
参数
void selectCategory(BuildContext ctx) {
Navigator.of(ctx).pushNamed(CategoryMealsScreen.routeName,
arguments: {
'id': id,
'title': title,
});
和地图
Widget build(BuildContext context) {
final routeArg = ModalRoute.of(context)?.settings.arguments as Map<String, String>; // the question mark is needed but I don't know why..!
final categoryId = routeArg["id"];
final categoryTitle = routeArg["title"];
final categoryMeals = DUMMY_MEALS.where((meal){
return meal.categories.contains(categoryId);
}).toList();
完整错误
构建 CategoryMealsScreen 时抛出了以下 _CastError(脏,依赖项:[_ModalScopeStatus],状态:_CategoryMealsScreenState#b3c64): type 'String' 不是 type cast
中 type 'Map<String, String>' 的子类型导致错误的相关小部件是: CategoryMealsScreen file:///C:/Users/DELL/AndroidStudioProjects/meal_app/lib/main.dart:37:50 抛出异常时,这是堆栈: #0 _CategoryMealsScreenState.build(包:meal_app/screens/category_meals_screen.dart:16:65) #1 StatefulElement.build(包:flutter/src/widgets/framework.dart:4691:27) #2 ComponentElement.performRebuild(包:flutter/src/widgets/framework.dart:4574:15) #3 StatefulElement.performRebuild(包:flutter/src/widgets/framework.dart:4746:11) #4 Element.rebuild(包:flutter/src/widgets/framework.dart:4267:5)
演示
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, routes: {
'/': (context) => CategoriesScreen(), // the HomePage
CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),
});
}
}
class CategoriesScreen extends StatefulWidget {
CategoriesScreen({Key? key}) : super(key: key);
@override
_MyWidgetXState createState() => _MyWidgetXState();
}
class _MyWidgetXState extends State<CategoriesScreen> {
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.of(context)
.pushNamed(CategoryMealsScreen.routeName, arguments: {
'id': "id 1",
'title': "title",
});
},
child: Text("pop"),
),
],
),
);
}
}
class CategoryMealsScreen extends StatefulWidget {
static final String routeName = "/asdasd";
CategoryMealsScreen({Key? key}) : super(key: key);
@override
_MyW2State createState() => _MyW2State();
}
class _MyW2State extends State<CategoryMealsScreen> {
@override
Widget build(BuildContext context) {
final routeArg = ModalRoute.of(context)?.settings.arguments as Map<String,
String>; // the question mark is needed but I don't know why..!
final categoryId = routeArg["id"];
final categoryTitle = routeArg["title"];
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("pop, $categoryTitle $categoryId"),
),
),
);
}
}