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?
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"]);
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With