Flutter 颤振:实例成员';星星';can';不能在初始值设定项中访问。错误
当我看到上面的链接页面时Flutter 颤振:实例成员';星星';can';不能在初始值设定项中访问。错误,flutter,dart,Flutter,Dart,当我看到上面的链接页面时 import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart' show debugPaintSizeEnabled; void main() { debugPaintSizeEnabled = true; // Remove to suppress visual layout runApp(MyApp()); } class MyApp extends Stat
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
void main() {
debugPaintSizeEnabled = true; // Remove to suppress visual layout
runApp(MyApp());
}
class MyApp extends StatelessWidget {
Widget stars=Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.black),
Icon(Icons.star, color: Colors.black),
Icon(Icons.star, color: Colors.black),
],
);
Widget ratings = Container(
padding: EdgeInsets.all(20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
stars,
Text(
'130 Reviews',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w800,
fontFamily: 'Roboto',
letterSpacing: 0.5,
fontSize: 20,
),
),
],
),
);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter layout demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter layout demo'),
),
// Change to buildColumn() for the other column example
body: ratings,
),
);
}
}
当我运行上面的代码时
The instance member 'stars' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
我得到了上面的错误
暂时
==>使评级和星级成为一项功能
==>使ratings和stars变量成为build()方法的局部变量
当我如上所述更改它时,错误消失了,但我不确定上面的代码为什么会出现错误。
原因是什么?您无法访问它定义的方法。若要使用,请在构建下方(即返回函数上方)添加您的方法
将变量移动到build方法中,或者您可以将它们定义为getter
导入“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”show debugPaintSizeEnabled;
void main(){
debugPaintSizeEnabled=true;//删除以抑制可视布局
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
Widget get stars=>行(
mainAxisSize:mainAxisSize.min,
儿童:[
图标(Icons.star,颜色:Colors.green[500]),
图标(Icons.star,颜色:Colors.green[500]),
图标(Icons.star,颜色:Colors.black),
图标(Icons.star,颜色:Colors.black),
图标(Icons.star,颜色:Colors.black),
],
);
Widget get ratings=>Container(
填充:边缘设置。全部(20),
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
星星,
正文(
“130次审查”,
样式:TextStyle(
颜色:颜色,黑色,
fontWeight:fontWeight.w800,
fontFamily:“机器人”,
字母间距:0.5,
尺寸:20,
),
),
],
),
);
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振布局演示”,
家:脚手架(
appBar:appBar(
标题:文本(“颤振布局演示”),
),
//将另一列示例更改为buildColumn()
主体:评级,
),
);
}
}
原因:
在dart中,不能创建类级变量,因为它依赖于另一个变量
另一种方法是将stars
变量定义为static变量:
导入“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”show debugPaintSizeEnabled;
void main(){
debugPaintSizeEnabled=true;//删除以抑制可视布局
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
静态窗口小部件星形=行(
mainAxisSize:mainAxisSize.min,
儿童:[
图标(Icons.star,颜色:Colors.green[500]),
图标(Icons.star,颜色:Colors.green[500]),
图标(Icons.star,颜色:Colors.black),
图标(Icons.star,颜色:Colors.black),
图标(Icons.star,颜色:Colors.black),
],
);
小部件评级=容器(
填充:边缘设置。全部(20),
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
星星,
正文(
“130次审查”,
样式:TextStyle(
颜色:颜色,黑色,
fontWeight:fontWeight.w800,
fontFamily:“机器人”,
字母间距:0.5,
尺寸:20,
),
),
],
),
);
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振布局演示”,
家:脚手架(
appBar:appBar(
标题:文本(“颤振布局演示”),
),
//将另一列示例更改为buildColumn()
主体:评级,
),
);
}
}
谢谢您的回复。在dart中,不能创建类级变量,因为它依赖于另一个变量↑ 官方文件(或类似文件)是否对此进行了解释?
Widget stars=Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.green[500]),
Icon(Icons.star, color: Colors.black),
Icon(Icons.star, color: Colors.black),
Icon(Icons.star, color: Colors.black),
],
);
Widget ratings = Container(
padding: EdgeInsets.all(20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
stars,
Text(
'130 Reviews',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w800,
fontFamily: 'Roboto',
letterSpacing: 0.5,
fontSize: 20,
),
),
],
),
);