PeakGen
PeakGen

Reputation: 23035

Flutter: Why am I getting the error "Route builders must never return null"?

I am new to flutter and below is my code

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

When I click on the button, I get the following error. If there is no error, I should be navigating into another page "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.

What is the problem here?

Upvotes: 5

Views: 10509

Answers (4)

Akshay B S Kumar
Akshay B S Kumar

Reputation: 1

  Navigator.push(context,
                  MaterialPageRoute(builder: (BuildContext context) {
                PageOne();
              }));

Instead of just instantiating the page to be pushed in the builder method ,add it as a return param

Navigator.push(context,
                  MaterialPageRoute(builder: (BuildContext context) {
              return  PageOne();
              }));

Upvotes: 0

user969068
user969068

Reputation: 2943

move productsMap in initState() function not in constructor

  initState() {
    super.initState();
    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"});
  }

also your Navigator.push

Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => ProductPage(productsMap[index]["title"],
                productsMap[index]["imageUrl"])),
            );

Upvotes: 0

TruongSinh
TruongSinh

Reputation: 4866

You forget to return the widget in your builder function

                  MaterialPageRoute(builder: (BuildContext context) {
                return ProductPage(productsMap[index]["title"],
                    productsMap[index]["imageUrl"]);
              }));

or if you prefer to use lambda function, the syntax would be:

                MaterialPageRoute(
                  builder: (BuildContext context) => ProductPage(
                      productsMap[index]["title"],
                      productsMap[index]["imageUrl"]),
                ),

Upvotes: 0

Marcos Boaventura
Marcos Boaventura

Reputation: 4741

As the error message says, you're returning nothing in your route builder. You miss return statement

Navigator.push(context,
                  MaterialPageRoute(builder: (BuildContext context) {
                return ProductPage(productsMap[index]["title"], // you miss return here!
                    productsMap[index]["imageUrl"]);
              }));

Upvotes: 5

Related Questions