未定义命名参数“home” - Flutter

Posted

技术标签:

【中文标题】未定义命名参数“home” - Flutter【英文标题】:The named parameter 'home' isn't defined - Flutter 【发布时间】:2020-12-02 22:56:12 【问题描述】:

我在 Flutter 项目中遇到问题。当我尝试在脚手架中调用'home'-statement 时,它会抛出一个错误,指出The named parameter 'home' isn't defined。我已经尝试了很多,但我没有找到任何解决此问题的方法。

另外,我最近开始研究 Flutter,这是我的第一个项目。所以请指导我正确的方法,因为我正处于 Flutter 体验的学习阶段。提前致谢。

文件结构:

 lib/
 ---components/
 ------appScaffold.dart
 ---pages/
 ------home.dart 
 main.dart

我已经实现了如下代码:-

main.dart

import 'package:flutter/material.dart';
import 'package:project/pages/home.dart';
import 'package:project/components/appScaffold.dart';

void main() => runApp(App());

class App extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return AppScaffold(
      home: HomePage(), //!Here's the problemt with 'home'
    );
  

home.dart

import 'package:project/main.dart';
import 'package:flutter/material.dart';
import 'package:bubble_bottom_bar/bubble_bottom_bar.dart';

class HomePage extends StatefulWidget 
  @override
  _HomePageState createState() => _HomePageState();


class _HomePageState extends State<HomePage> 
  int currentPageIndex = 0;

  void changePage(int newPageIndex) 
    setState(() 
      currentPageIndex = newPageIndex;
    );
  

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text('Title'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () ,
        child: Icon(Icons.add),
        backgroundColor: Colors.red,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      bottomNavigationBar: BubbleBottomBar(
        opacity: .2,
        currentIndex: currentPageIndex,
        onTap: changePage,
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
        elevation: 8,
        fabLocation: BubbleBottomBarFabLocation.end, //new
        hasNotch: true, //new
        hasInk: true, //new, gives a cute ink effect
        inkColor: Colors.black12, //optional, uses theme color if not specified
        items: <BubbleBottomBarItem>[
          BubbleBottomBarItem(
              //Home
              backgroundColor: Colors.red,
              icon: Icon(
                Icons.home,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.home,
                color: Colors.red,
              ),
              title: Text("text")),
          BubbleBottomBarItem(
              backgroundColor: Colors.deepPurple,
              icon: Icon(
                Icons.menu,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.menu,
                color: Colors.deepPurple,
              ),
              title: Text("text")),
          BubbleBottomBarItem(
              //Icon3
              backgroundColor: Colors.indigo,
              icon: Icon(
                Icons.menu,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.menu,
                color: Colors.indigo,
              ),
              title: Text("text")),
          BubbleBottomBarItem(
              //Icon 4
              backgroundColor: Colors.green,
              icon: Icon(
                Icons.menu,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.menu,
                color: Colors.green,
              ),
              title: Text("text"))
        ],
      ),
    );
  

appScaffold.dart

import 'package:flutter/material.dart';
import 'package:project/main.dart';
import 'package:project/pages/home.dart';

class AppScaffold extends StatelessWidget 
  AppScaffold(this.home);
  final Widget home;

  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'MoneyTracker',
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        primaryColor: Colors.indigo,
        secondaryHeaderColor: Colors.indigo,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: home,
    );
  

【问题讨论】:

remove home: - AppScaffold ctor 没有这样的命名参数 @pskink 那么他的应用程序不会显示任何内容。此评论没有帮助。 只使用AppScaffold(HomePage())而不使用home: 【参考方案1】:

问题在于AppScaffold 类中的构造函数。 当你想命名一个参数时,你必须把它放在花括号中。 这看起来像:

import 'package:flutter/material.dart';
import 'package:project/main.dart';
import 'package:project/pages/home.dart';

class AppScaffold extends StatelessWidget 
  AppScaffold(this.home);
  final Widget home;

  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'MoneyTracker',
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        primaryColor: Colors.indigo,
        secondaryHeaderColor: Colors.indigo,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: home,
    );
  

当您想创建一个同时具有命名参数和位置参数的构造函数时。它看起来像:

MyClass(this.positional, this.named, this.alsoNamed);

Here 你可以阅读更多关于 dart 中命名参数的信息。

【讨论】:

以上是关于未定义命名参数“home” - Flutter的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:我正在尝试使用扩展类包装 Text 小部件,但出现“未定义命名参数‘child’”错误

002 flutter的路由管理--命名路由

Flutter 6种构造函数详解

使用颤振将多个参数传递给命名路由

Flutter 通过命名路由导航直接传递参数

Flutter 错误:没有名为“keyboardType”的命名参数。键盘类型:TextInputType.text