Home>

https://flutter.dev/docs/development/ui/layout#nesting-rows-and-columns
I'm doing the above page

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, 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 you run

The instance member'stars' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expressio

I got the error, but I'm not sure what's wrong.
For the time being, when I made ratings and stars a function, the error disappeared, but
What causes the above code to give an error?


2020/11/1/12: 13 postscript
Sample code

import'package: flutter/material.dart';
// import'package: flutter/rendering.dart' show debugPaintSizeEnabled;
void main () {
  // debugPaintSizeEnabled = true;// Remove to suppress visual layout
  // ↓ (1) → Result: x = 10, y = 10
  // runApp (MyApp._internal (x: 10, y: 10));
  // (2) → Result: x = 20, y = 0
  // runApp (MyApp.red (20));
  // (3) → Result: x = 30, y = 0
  runApp (MyApp (30));
}
class MyApp extends StatelessWidget {
  MyApp._internal ({this.x, this.y});// generative constructor
  MyApp.red (int x): this._internal (x: x, y: 0);// Redirect constructor
  factory MyApp (x) {// factory constructor
    MyApp _ma = MyApp._internal (x: x, y: 0);
    return _ma;
  }
  final int x;
  final int y;
  @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: Row (children: [Text ("x = $x"), Text ("y = $y")]),
      ),
    );
  }
}
  • Answer # 1

    Of instance membersratingAlso a member of the instance at widget initializationstarsI think it's because I'm trying to reference a widget. As a temporary solutionstarsCan be considered as a class member (add static), but personallyfactoryI think it would be better to use.

    Less than,factoryThis is an example using.

    class MyApp extends StatelessWidget {
      MyApp._internal ();
      Widget stars;
      Widget ratings;
      factory MyApp () {
        MyApp _myApp = MyApp._internal ();
        _myApp.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),
          ],,
        );
        _myApp.ratings = Container (
          padding: EdgeInsets.all (20),
          child: Row (
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              _myApp.stars,
              Text (
                '130 Reviews',
                style: TextStyle (
                  color: Colors.black,
                  fontWeight: FontWeight.w800,
                  fontFamily:'Roboto',
                  letterSpacing: 0.5,
                  fontSize: 20, 20,
                ),
              ),
            ],,
          ),
        );
        return _myApp;
      }
      @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,
          ),
        );
      }
    }

    OriginallyMyAppMembers ofstars``ratingsButfinalBecause notMyAppIsStatefulWidgetI think it's better to use it in.