2

I can't use widget.message inside _MenuStatefulWidgetState, the error said The getter 'message' isn't defined for the class 'MenuStatefulWidget.

I think because I'm using a FutureBuilder, and trying to access widget.message inside builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) How can I resolve this?

class MenuStatefulWidget extends StatefulWidget {
  MenuStatefulWidget({Key key, String message}) : super(key: key);

  @override
  _MenuStatefulWidgetState createState() => _MenuStatefulWidgetState();
}

class _MenuStatefulWidgetState extends State<MenuStatefulWidget> {
  Widget build(BuildContext context) {

    String storeID =
        Provider.of<AppData>(context, listen: true).selectedStoreId;
    String storeMenuFilename = 'store_$storeID.json';

    Future _menuDataFuture = getFile(storeMenuFilename);

    return FutureBuilder<dynamic>(
      future: _menuDataFuture,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        List<Widget> children;

        if (snapshot.hasData) {
          children = [];

          var storeData = snapshot.data;

          Provider.of<AppData>(context, listen: false).setStoreData(storeData);

          var menu = storeData['menu'];
          var itemOption = storeData['option'];

          for (var category in menu) {
            (category as Map<String, dynamic>).forEach((key, value) {
              children.add(CategoryWidget(name: key));
              for (var course in value) {
                children.add(LineWidget(course, itemOption));
              }
            });
          }
        } else if (snapshot.hasError) {
          children = <Widget>[
            Icon(
              Icons.error_outline,
              color: Colors.red,
              size: 60,
            ),
          ];
        } else {
              if (widget.message != null){
                  children.add(
                    Container(
                      margin: topBottomMargin,
                      child: Text(widget.message,
                      ),
                    ),
                  );
              }

          children.add(
            Container(
              padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: Center(
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CircularProgressIndicator(),
                  ],
                ),
              ),
            ));
        }


        return Center(
          child: ListView(
            children: children,
          ),
        );
      },
    );
  }
}

2 Answers 2

4

You need to create a variable named message in your widget and then provide reference to it as follows:

class MenuStatefulWidget extends StatefulWidget {
  String message;

  MenuStatefulWidget({Key key, this.message}) : super(key: key);

  @override
  _MenuStatefulWidgetState createState() => _MenuStatefulWidgetState();
}

using the above code we declared the variable named "message" in MenuStatefulWidget and it will be assigned the value passed as a param to MenuStatefulWidget. Now you can get value of message using

widget.message
Sign up to request clarification or add additional context in comments.

1 Comment

what is the advantage of using widget.message over 'this.message' ? Where is the use of widget.variable documented in flutter site ?
0
class MenuStatefulWidget extends StatefulWidget {
  String message;

  MenuStatefulWidget({Key key, String message}) : super(key: key);

  @override
  _MenuStatefulWidgetState createState() => _MenuStatefulWidgetState();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.