【问题标题】:type 'String' is not a subtype of type 'Map<String, String>' in type cast“String”类型不是类型转换中“Map<String, String>”类型的子类型
【发布时间】:2021-11-07 04:23:12
【问题描述】:

当我单击 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(脏,依赖项:[_ModalScopeStatus],状态:_CategoryMealsScreenState#b3c64)时引发了以下 _CastError: “String”类型不是类型转换中“Map”类型的子类型

相关的导致错误的小部件是: 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 (package:flutter/src/widgets/framework.dart:4691:27) #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) #3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) #4 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)

【问题讨论】:

  • 参数很可能是一个字符串。打印routeArg 并检查数据的样子。您还可以向我们展示如何将参数添加到该路由。
  • 你能说明你是如何传递数据的吗?
  • 似乎 ModalRoute.of(context)?.settings.arguments 没有返回地图。检查一下。
  • final routeArg = ModalRoute.of(context)?.settings.arguments as Map&lt;String, String&gt;; 内部构建方法。如果不放在 build 方法中。
  • @YeasinSheikh 它已经在 Build 方法中

标签: flutter flutter-test


【解决方案1】:

演示


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2021-12-12
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多