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

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

概述我是新手,下面是我的代码homepage.dartimport'package:flutter/material.dart';import'./product_page.dart';classHomepageextendsStatelessWidget{@overrideWidgetbuild(BuildContextcontext){//TODO:implementbuildreturnSca

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

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:为什么会出现错误“路由构建器绝不能返回null”?全部内容,希望文章能够帮你解决android-Flutter:为什么会出现错误“路由构建器绝不能返回null”?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存