gpt4 book ai didi

android - Flutter:为什么我收到错误 "Route builders must never return null"?

转载 作者:IT老高 更新时间:2023-10-28 12:45:40 25 4
gpt4 key购买 nike

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

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

关于android - Flutter:为什么我收到错误 "Route builders must never return null"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com