简体   繁体   English

如何使用 Dart 解决 Flutter 应用程序中的路由错误?

[英]How do i solve route error in Flutter app with Dart?

I am working on a flutter app with Dart as backend language.我正在使用 Dart 作为后端语言开发 Flutter 应用程序。 While trying to navigate through a page using在尝试使用浏览页面时
Navigator.of(context).NamedRoute()
I got an error message我收到一条错误消息
Could not find a generator for route RouteSettings("/item-category", {id: Vegetables, title: Vegetables}) in the _WidgetsAppState. I have done the necessary imports in each widget but haven't mentioned in the code.我已经在每个小部件中完成了必要的导入,但在代码中没有提到。


My main.dart file is: 我的 main.dart 文件是:
class ItemListScreen extends StatefulWidget {
  static const routeName = '/item-list';

  @override
  _ItemListScreenState createState() => _ItemListScreenState();
}

class _ItemListScreenState extends State<ItemListScreen> {
  @override
  Widget build(BuildContext context) {
    final routeArgs =
        ModalRoute.of(context).settings.arguments as Map<String, String>;
    final categoryTitle = routeArgs['title'];
    final categoryId = routeArgs['id'];
    final categoryItems = ITEMS_DATA.where((item) {
      return item.categories.contains(categoryId);
    }).toList();
    return Scaffold(
      appBar: AppBar(
        title: Text(categoryTitle),
      ),
      body: ListView.builder(
        itemBuilder: (ctx, index) {
          return EachItem(
            id: categoryItems[index].id,
            title: categoryItems[index].title,            
          );
        },
        itemCount: categoryItems.length,
      ),
    );
  }
}

The screen for displaying the Items screen is in class ItemListScreen and the code is as:显示 Items 屏幕的屏幕在 ItemListScreen 类中,代码如下:

enum Condition {
  Fresh,
  Frozen,
  Immediate_sale,
}

enum Status {
  Available,
  Pending,
  Sold,
}

class Item {
  final String id;
  final List<String> categories;
  final String title;
  final String imageUrl;
  final double price;
  final String priceType;
  // final DateTime harvestDate;
  final Condition condition;
  final Status status;
  final bool isOrganic;
  final Map location;

  const Item({
    @required this.id,
    @required this.categories,
    // may be more than one category
    @required this.title,
    @required this.imageUrl,
    @required this.price,
    @required this.priceType,
    // eg.per kg/per 12/per 100
    // @required this.harvestDate,
    @required this.condition,
    // fresh/frozen/should go immediately
    @required this.status,
    // sold/pending/available
    @required this.location,
    @required this.isOrganic,
  });
}

The model design of the Each items is class Item. Each item 的模型设计是类Item。

class EachItem extends StatelessWidget {
  final String id;
  final String title;
  /*final String imageUrl;
  final double price;
  final String priceType;
  // final DateTime harvestDate;
  final Condition condition;
  final Status status;
  final bool isOrganic;*/

  EachItem({
    @required this.id,
    // may be more than one category
    @required this.title,
    /*@required this.imageUrl,
    @required this.price,
    @required this.priceType,
    // eg.per kg/per 12/per 100
    // @required this.harvestDate,
    @required this.condition,
    // fresh/frozen/should go immediately
    @required this.status,
    // sold/pending/available
    @required this.isOrganic,*/
  });

  void selectItem(BuildContext context) {
    Navigator.of(context).pushNamed(
      ItemListScreen.routeName,
      arguments: id,
    );
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => selectItem(context),
      child: Card(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
        ),
        elevation: 4,
        margin: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            Stack(
              children: <Widget>[
                Positioned(
                  bottom: 20,
                  right: 10,
                  child: Container(
                    width: 300,
                    color: Colors.black54,
                    padding: EdgeInsets.symmetric(
                      vertical: 5,
                      horizontal: 20,
                    ),
                    child: Text(
                      title,
                      style: TextStyle(
                        fontSize: 26,
                        color: Colors.white,
                      ),
                      softWrap: true,
                      overflow: TextOverflow.fade,
                    ),
                  ),
                )
              ],
            ),                
          ],
        ),
      ),
    );
  }
}

The widget for each item in the children of row builder of the screen is as follows:屏幕的行构建器的子项中每个项目的小部件如下:

const ITEMS_DATA = const [
  Item(
    id: '1',
    title: 'Apple',
    categories: ['Fruits'],
    price: 3.5,
    priceType: 'per kg',
    // harvestDate: DateTime.parse('2012-02-27 13:27:00'),
    condition: Condition.Fresh,
    status: Status.Available,
    isOrganic: true,
    location: null,
    imageUrl:
        'https://cdn.pixabay.com/photo/2018/04/09/18/26/asparagus-3304997_1280.jpg',
  ),
];

ATM I haven't connected to the firebase and used dummy data as data provider in my code. ATM 我没有连接到 firebase 并在我的代码中使用虚拟数据作为数据提供者。 Dummy data for one item is as follows:一项的虚拟数据如下:

 const ITEMS_DATA = const [ Item( id: '1', title: 'Apple', categories: ['Fruits'], price: 3.5, priceType: 'per kg', // harvestDate: DateTime.parse('2012-02-27 13:27:00'), condition: Condition.Fresh, status: Status.Available, isOrganic: true, location: null, imageUrl: 'https://cdn.pixabay.com/photo/2018/04/09/18/26/asparagus-3304997_1280.jpg', ), ];

ItemListScreen.routeName: (ctx) => ItemListScreen(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为Flutter应用创建纯Dart程序包? - How do I create a pure Dart package for a Flutter app? 如何解决此错误我正在尝试将我的 flutter 应用程序添加到 web - how do i solve this error am trying to add my flutter app to web 如何在运行 flutter 应用程序时解决 'dart:ui' 问题? - How to solve 'dart:ui' problem while running flutter app? 如何解决MAC和flutter app上的Provisioning Profile问题 - How do I solve the problem of Provisioning Profile on MAC and flutter app 你如何解决 Flutter/Dart 中的“右括号综合症”? - How do you solve the "closing brackets syndrome" in Flutter/Dart? 我在flutter中运行第一个应用程序时遇到此错误,如何解决? - I face this error while running the first app in flutter , how to solve it? 如何解决此列表<dynamic> Flutter/Dart 中的类型错误</dynamic> - How to solve this List<dynamic> type error in Flutter/Dart 如何仅使用 dart 代码解决 flutter web api cors 错误? - How to solve flutter web api cors error only with dart code? 如何检测应用程序是否使用 Flutter web 中的路由启动? - How do I detect if app was launched with a route in Flutter web? 如何在VS Code中的调试中以dart v2模式运行我的flutter应用程序(--preview-dart-2选项) - How do I run my flutter app in dart v2 mode in debug in VS Code (--preview-dart-2 option)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM