【问题标题】:How can I add method to StatefulWidget in flutter?如何在颤振中向 StatefulWidget 添加方法?
【发布时间】:2022-01-04 09:45:25
【问题描述】:

我想调用这个方法但不能。我的目的是 从文本变量中获取字符串 使用字符串中的单词制作列表 显示列表。 我希望通过这个来做到这一点。有没有其他方法或者我可以这样做吗?

class SecondRoute extends StatefulWidget {
  String text;
  //const SecondRoute({Key? key}) : super(key: key);
  SecondRoute({Key? key, required this.text}) : super(key: key);

  @override
  _SecondRouteState createState() => _SecondRouteState();


}

class _SecondRouteState extends State<SecondRoute> {
  String newT ="";
  

  List breakText(){
   newT = widget.text;
    var x = newT.split(" ");
    print(x);
    bool res = x.remove("");
    while(res == true){
      res = x.remove("");
    }
    print(x);
    return x;

  }
  List wordlist = breakText();// can not call this method
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Second Route"),
      ),
      body: Center(
        child: Text(
          "hhhh",
          // text,
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart inheritance parent-child parent


    【解决方案1】:

    你应该移动“List wordlist = breakText();”进入 initState() 或 build() 方法。

    【讨论】:

      【解决方案2】:

      1-定义列表:

      List wordlist ;
      

      2-初始化:

       @override
        void initState(){
      wordlist = breakText();
      }
      

      【讨论】:

        【解决方案3】:

        调用_SecondRouteState时需要初始化数据

        class SecondRoute extends StatefulWidget {
          String text;
          //const SecondRoute({Key? key}) : super(key: key);
          SecondRoute({Key? key, required this.text}) : super(key: key);
        
          @override
          _SecondRouteState createState() => _SecondRouteState();
        
        
        }
        
        class _SecondRouteState extends State<SecondRoute> {
          String newT ="";
          
        
          List breakText(){
           newT = widget.text;
            var x = newT.split(" ");
            print(x);
            bool res = x.remove("");
            while(res == true){
              res = x.remove("");
            }
            print(x);
            return x;
        
          }
          List wordlist; // declare your list
          @override
          void initState() {
            super.initState();
            wordlist = breakText(); // initialize from here
        
          }
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: const Text("Second Route"),
              ),
              body: Center(
                child: Text(
                  "hhhh",
                  // text,
                  style: TextStyle(fontSize: 24),
                ),
              ),
            );
          }
        }
        
        

        【讨论】:

        • @jahidul_Islam 如果我想在正文中显示列表,可以吗?
        • 是的,可以使用 Text 代替 ListView
        猜你喜欢
        • 2020-10-02
        • 2019-04-07
        • 2019-03-20
        • 2019-11-03
        • 2021-06-05
        • 2020-05-14
        • 2023-03-18
        • 1970-01-01
        • 2021-10-12
        相关资源
        最近更新 更多