简体   繁体   中英

Flutter/Hive - How to write Widget positional arguments?

(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. 在此处输入图像描述

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) 
    );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM