简体   繁体   English

Flutter/Hive - 如何编写 Widget 位置 arguments?

[英]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.在这段代码中,我需要将buildApiBox调用到page_ApiBox class。但我还需要传递apibox.api_key ,它已经连接到另一个 class 中的变量。

Any advice is appreciated.任何建议表示赞赏。 I am new to Flutter/Dart.我是 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.如您所见,第 90 行的apibox未定义。 在此处输入图像描述

You can pass a parameter in the page_ApiBox constructor like this here.您可以在此处像这样在page_ApiBox构造函数中传递参数。

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.然后,如果你想访问你的变量,你需要使用widget. since it is a StatefulWidget因为它是一个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) 
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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