type 'String'不是类型'Map<String, String>'



当我点击一个按钮在我的扑动应用程序我得到了一个错误有人有解决办法吗?!

routes:{
'/':(context) => CategoriesScreen(), // the HomePage
CategoryMealsScreen.routeName: (context) => CategoryMealsScreen(),}

参数

void selectCategory(BuildContext ctx) {
Navigator.of(ctx).pushNamed(CategoryMealsScreen.routeName,
arguments: {
'id': id,
'title': title,
});

和map

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(dirty, dependencies: [_ModalScopeStatus], state: _CategoryMealsScreenState#b3c64)时抛出的_CastError:类型'String'不是类型'Map<String,>'的子类型

相关的导致错误的小部件是:CategoryMealsScreen文件:///C:/用户/DELL/AndroidStudioProjects meal_app/lib/main.dart: 37:50当抛出异常时,这是堆栈:# 0 _CategoryMealsScreenState。构建(包:meal_app/屏幕/category_meals_screen.dart: 16:65)# 1 StatefulElement。构建(包:颤振/src/widget/framework.dart: 4691:27)# 2 ComponentElement。performRebuild(包:颤振/src/widget/framework.dart: 4574:15)# 3 StatefulElement。performRebuild(包:颤振/src/widget/framework.dart: 4746:11)# 4元素。重建(包:颤振/src/widget/framework.dart: 4267:5)

Demo


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"),
),
),
);
}
}

相关内容

  • 没有找到相关文章