android-Flutter:为什么会出现错误“路由构建器绝不能返回null”?

我是新手,下面是我的代码

homepage.dart

import 'package:flutter/material.dart';

import './product_page.dart';

class Homepage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Choco Factory"),
      ),
      body: HomepageUI(),
    );
  }
}

class HomepageUI extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomepageUIState();
  }
}

class _HomepageUIState extends State<HomepageUI> {
  List<Map<String, String>> productsMap = [];

  _HomepageUIState() {
    productsMap
        .add({"title": "Cappuccino", "imageUrl": "assets/cappuccino.jpg"});
    productsMap.add(
        {"title": "Chocolate Cake", "imageUrl": "assets/chocolate_cake.jpg"});
    productsMap
        .add({"title": "Chocolates", "imageUrl": "assets/chocolates.jpg"});
    productsMap.add(
        {"title": "Hot Chocolate", "imageUrl": "assets/hot_chocolate.jpg"});
    productsMap.add({"title": "Naougat", "imageUrl": "assets/nougat.jpg"});
    productsMap.add(
        {"title": "White Chocolate", "imageUrl": "assets/white_chocolate.jpg"});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Text("Select Your Choco!"),
        Expanded(
            child: ListView.builder(
          itemBuilder: _listBuilder,
          itemCount: productsMap.length,
        )

            //child: Image.asset("assets/chocolates.jpg"),
            )
      ],
    );
  }

  Widget _listBuilder(BuildContext context, int index) {
    return Card(
      child: Column(
        children: <Widget>[
          Image.asset(productsMap[index]["imageUrl"]),
          Text(productsMap[index]["title"]),
          RaisedButton(
            child: Text("Details"),
            color: Colors.green,
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (BuildContext context) {
                ProductPage(productsMap[index]["title"],
                    productsMap[index]["imageUrl"]);
              }));
            },
          )
        ],
      ),
    );
  }
}

product_page.dart

import 'package:flutter/material.dart';

class ProductPage extends StatelessWidget {
  final String  title, imageUrl;

  ProductPage(this.title,this.imageUrl);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Choco Factory"),
      ),
      body: ProductPageUI(title, imageUrl),
    );
  }
}

class ProductPageUI extends StatefulWidget {

  String title, imageUrl;

  ProductPageUI(this.title, this.imageUrl);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ProductPageUIState();
  }
}

class _ProductPageUIState extends State<ProductPageUI> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return _productPageUIBuilder(widget.title, widget.imageUrl);
  }

  Widget _productPageUIBuilder(String title, String imageUrl) {
    return Column(
      children: <Widget>[
        Text(title),
        Image.asset(imageUrl),
        Text(
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean semper sodales nisi, ac ornare nisl ullamcorper vel. Aliquam nibh libero, consequat in arcu molestie, congue scelerisque elit. Integer eu ex in tellus iaculis egestas. Aliquam a molestie ante. Etiam eget magna id neque suscipit sollicitudin. Phasellus dolor erat, sagittis ut felis quis, faucibus finibus est. Aenean nunc justo, venenatis nec urna a, vehicula lacinia odio. Ut molestie velit vitae augue pulvinar dignissim. Integer tempus nisi dignissim nisl rutrum venenatis at in leo."),
      ],
    );
  }
}

当我单击按钮时,出现以下错误.如果没有错误,我应该导航到另一个页面“ ProductPage”.

I/flutter (31115): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (31115): The following assertion was thrown building Builder(dirty):
I/flutter (31115): The builder for route "null" returned null.
I/flutter (31115): Route builders must never return null.

这里有什么问题?

解决方法:

如错误消息所述,您在路由生成器中未返回任何内容.你错过了退货声明

Navigator.push(context,
                  MaterialPageRoute(builder: (BuildContext context) {
                return ProductPage(productsMap[index]["title"], // you miss return here!
                    productsMap[index]["imageUrl"]);
              }));
上一篇:java – SXSSFWorkbook上的AutosizeColumns


下一篇:java-Primefaces datatable选定的行无法使用viewscope工作