简体   繁体   English

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

[英]The named parameter 'home' isn't defined - Flutter

I am facing an issue in my Flutter project.我的 Flutter 项目遇到问题。 When I try calling 'home'-statement in the scaffold, it throws an error stating The named parameter 'home' isn't defined .当我尝试在脚手架中调用 'home'-statement 时,它会抛出一个错误,指出The named parameter 'home' isn't defined I have tried a lot around, but I didn't found any solution to this issue.我已经尝试了很多,但我没有找到任何解决这个问题的方法。

Also, I have recently started working on Flutter and this is my first project.另外,我最近开始研究 Flutter,这是我的第一个项目。 So please guide me through the correct approach as I am in the learning phase of the Flutter experience.所以请指导我正确的方法,因为我正处于 Flutter 体验的学习阶段。 Thanks in advance.提前致谢。

Files Structure:文件结构:

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

I have implemented the code as below:-我已经实现了如下代码: -

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主页.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 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,
    );
  }
}

The problem is the constructor in your AppScaffold class.问题是您的AppScaffold class 中的构造函数。 When you want to name an argument you have to put it into curly braces.当你想命名一个参数时,你必须把它放在花括号中。 This would then look like:这看起来像:

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

When you want to create a constructor with both, named and positional arguments.当您想创建一个同时具有命名和位置 arguments 的构造函数时。 It would look like:它看起来像:

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

Here you can read more about Named Arguments in dart. 在这里您可以阅读更多关于 dart 中命名为 Arguments 的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:未定义命名参数错误 - Flutter : The named parameter isn't defined Error Flutter:未定义命名参数“ decoration” - Flutter: The named parameter 'decoration' isn't defined 错误:未定义命名参数“child” - error: The named parameter 'child' isn't defined 未定义命名参数“onSelectNotification” - The named parameter 'onSelectNotification' isn't defined 怎么修? 错误:未定义命名参数“child”。 (undefined_named_parameter 在 [application_1] - How to fix? error: The named parameter 'child' isn't defined. (undefined_named_parameter at [application_1] 未定义命名参数“inputDetailsFromUser”。 尝试将名称更正为现有命名参数的名称 - The named parameter 'inputDetailsFromUser' isn't defined. Try correcting the name to an existing named parameter's name 未为 flutter WebViewPlus 定义“canGoBack”方法 - 'canGoBack' method isn't defined for flutter WebViewPlus 方法连接未定义flutter sqflite - the method join isn't defined flutter sqflite Flutter:未为类“ FirebaseAuth”定义方法“ signInWithGoogle” - Flutter:The method 'signInWithGoogle' isn't defined for the class 'FirebaseAuth' 没有为类型“Type”定义运算符“[]”。 Flutter 列表视图构建器 - The operator '[]' isn't defined for the type 'Type'. Flutter ListView Builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM