[英]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:-我已经实现了如下代码: -
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'
);
}
}
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"))
],
),
);
}
}
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.