Flutter:无法在初始化程序中访问实例成员"stars".错误 [英] Flutter: The instance member'stars' can't be accessed in an initializer. Error
问题描述
https://flutter.dev/docs/development/ui/layout#nesting-rows-and-columns
当我查看上面的链接页面时
When I was looking at the linked page above
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
我收到上述错误.
暂时
==>使评级和星号起作用.
==> Make ratings and stars a function.
==>将等级和星标变量设为build()方法的局部变量.
==> Make the ratings and stars variables local variables of the build () method.
当我如上所述进行更改时,错误消失了,但是我不确定为什么上面的代码给出了错误.是什么原因?
When I changed it as above, the error disappeared, but I'm not sure why the above code gives an error. What's the reason?
推荐答案
将变量移至构建方法中,也可以将其定义为getter
move variables into build method, or you can defined theirs as getter
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 get 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 get 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,
),
);
}
}
原因:在dart中,您无法创建依赖于另一个变量的类级变量
reason : in dart you can't create a class level variable depends with another variable
另一种方法是将 stars
变量定义为 static 变量:
another way is define stars
variable as static variable :
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 {
static 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,
),
);
}
}
这篇关于Flutter:无法在初始化程序中访问实例成员"stars".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!