【问题标题】:Flutter route problem Could not find a generator for route RouteSettingFlutter路由问题找不到路由RouteSetting的生成器
【发布时间】:2021-05-29 09:15:42
【问题描述】:

我与它斗争了 2 个小时,但仍然没有创建解决方案,所以我来这里寻求帮助,昨天当我打开我的应用程序时,一切都很顺利,今天我改变了一点咬了我的代码,但我什至尝试删除它以检查它是否有问题,当我删除它时它仍然很糟糕。这是我在终端的问题

在处理手势时抛出以下断言: 找不到路线 RouteSettings("/Category-meals", {id: c1, title: Italian}) 的生成器 _WidgetsAppState。 确保您的根应用小部件提供了一种生成方式 这条路线。 按以下顺序搜索路线的生成器:

对于“/”路由,如果非空,则使用“home”属性。 否则,如果“路由”表具有路由条目,则使用该表。 否则,调用 onGenerateRoute。它应该为任何有效的路由返回一个非空值,而不是 由“家”和“路线”处理。 最后,如果所有其他方法都失败,则调用 onUnknownRoute。 不幸的是,未设置 onUnknownRoute。 这是我使用路由命令的文件,

main.dart

`import 'package:flutter/material.dart';

import './category_meals.dart';
import 'categories_screen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'DeliMeals',
      theme: ThemeData(
          primarySwatch: Colors.pink,
          accentColor: Colors.amber,
          canvasColor: Color.fromRGBO(255, 254, 229, 1),
          textTheme: ThemeData.light().textTheme.copyWith(
                bodyText1: TextStyle(
                  color: Color.fromRGBO(20, 51, 51, 1),
                ),
                bodyText2: TextStyle(
                  color: Color.fromRGBO(20, 51, 51, 1),
                ),
                headline6: TextStyle(
                  fontSize: 24,
                ),
              )),
      home: CategoriesScreen(),
      routes: {
        CategoryMeals.routeName: (ctx) => CategoryMeals(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DeliMeals'),
      ),
      body: Center(
        child: Text('Navigation Time!'),
      ),
    );
  }
}
`

category-meals.dart

`import 'package:flutter/material.dart';
import './dummy_data.dart';

class CategoryMeals extends StatelessWidget {
  static const routeName = 'Category-meals';
  /* final String categoryId;
  final String categoryTitle;
  
  CategoryMeals(this.categoryId, this.categoryTitle); */
  
  @override
  Widget build(BuildContext context) {
    final routesArgs = ModalRoute.of(context).settings.arguments as Map<String, String>;
    final categoryTitle = routesArgs['title'];
    final categoryId = routesArgs['id'];
    final categoryMeals = DUMMY_MEALS.where((meal) {
      return meal.categories.contains(categoryId);
    }).toList();
    return Scaffold(
      appBar: AppBar(
        title: Text(categoryTitle),
      ),
      body: ListView.builder(itemBuilder: (ctx, index) {
        return Text(categoryMeals[index].title);
      }, itemCount: categoryMeals.length,), 
    );
  }
}
`

category-items.dart

`import 'package:flutter/material.dart';
import './category_meals.dart';

class CategoryItem extends StatelessWidget {
  final String id;
  final String title;
  final Color color;

  CategoryItem(this.id, this.title, this.color);

  void selectCategory(BuildContext ctx) {
    Navigator.of(ctx).pushNamed('/Category-meals', arguments: {
      'id': id,
      'title': title
    });
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => selectCategory(context),
      splashColor: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.circular(15),
      child: Container(
        padding: const EdgeInsets.all(15),
        child: Text(
          title,
          style: Theme.of(context).textTheme.headline6,
        ),
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              color.withOpacity(0.7),
              color,
            ],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
          borderRadius: BorderRadius.circular(15),
        ),
      ),
    );
  }
}

如果有人知道解决方案,我将非常乐意提供帮助并解释问题所在,谢谢

【问题讨论】:

    标签: flutter routes


    【解决方案1】:

    您在 category-meals.dart 中有一个 routeName = 'Category-meals',而在 category-items.dart 中,您正试图浏览不存在的 routeName = '/Category-meals'。尝试将 category-items.dart 中的“Category-meals”更改为“/Category-meals”,应该可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 2020-07-12
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 2016-07-03
      相关资源
      最近更新 更多