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,
),
);
}
}
When I run the above code
The instance member 'stars' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
I get the above error.
For the time being
==> Make ratings and stars a function.
==> 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?