Flutter/Hive - How to write Widget positional arguments?

Viewed 36

(I am sorry I can only post a portion of my code, I hope what I have here is enough.)

In this code, I need to call buildApiBox into the page_ApiBox class. But I also need to pass apibox.api_key which is already connected to a variable in a different class.

Any advice is appreciated. I am new to Flutter/Dart.

(Part of) My Code:

class page_ApiBox extends StatefulWidget {
    const page_ApiBox({Key? key}) : super(key: key);

    @override
    State<page_ApiBox> createState() => _page_ApiBoxState();
  }

  class _page_ApiBoxState extends State<page_ApiBox> {


    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: buildApiBox(context, apibox)
      );
    }
  }

  Widget buildApiBox(BuildContext context, apiBox apibox) {
    return Scaffold(
        appBar: AppBar(
            title: Text('API BOX')
        ),
        body: Text(apibox.api_key) //I need to display what is inside the variable
    );
  }

Here is a photo of my code. As you can see, apibox in line 90 is undefined. enter image description here

1 Answers

You can pass a parameter in the page_ApiBox constructor like this here.

class page_ApiBox extends StatefulWidget {
    const page_ApiBox({Key? key, required this.apibox}) : super(key: key);
    final apiBox apibox;

    @override
    State<page_ApiBox> createState() => _page_ApiBoxState();
  }

Then if you want to get access to your variable you need to use widget. since it is a StatefulWidget

  class _page_ApiBoxState extends State<page_ApiBox> {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: buildApiBox(context, widget.apibox)
      );
    }
  }

  Widget buildApiBox(BuildContext context, apiBox apibox) {
    return Scaffold(
        appBar: AppBar(
            title: Text('API BOX')
        ),
        body: Text(apibox.api_key) 
    );
  }
Related