【问题标题】:How to split the text data after getting from api in FlutterFlutter中从api获取后如何拆分文本数据
【发布时间】:2019-12-12 06:03:35
【问题描述】:

从 api 调用获取后,我想拆分 Text 小部件中的文本数据。这是代码

Row(
children:<Widget>[
Text('result'),
Text(item[pos].asr), //getting the data from api call which is "22:00"
RaisedButton(
onpressed(){
print()// here i want to show the split text data which is "22" then "00" under "22"
}
),
]
)

【问题讨论】:

    标签: http flutter dart


    【解决方案1】:

    试一试:

    Row(
          children:<Widget>[
            Text('result'),
            Text(item[pos].asr),
            RaisedButton(
              onPressed: () {
                List<String> arr = item[pos].asr.split(':');
                int hour = int.parse(arr[0]);
                int minutes = int.parse(arr[1]);
                print(hour.toString() + " " + minutes.toString());
              },
            ),
          ]
      )
    

    【讨论】:

      【解决方案2】:

      您可以使用split 函数进行拆分:

      List<String> splitted = item[pos].asr.split(":"); // list containing 22 and 00.
      

      要打印 22,请使用 -> splitted[0]
      对于 00,使用 -> splitted[1]

      【讨论】:

      • Text(item[pos].asr) 的数据在我在移动屏幕上运行时显示为 22:00,您提到了“硬编码”声明的字符串。
      【解决方案3】:

      您可以将拆分后的值存储在列表中

      var string = "22:00"; 
      List splitedText = string.split(":");
      print(splitedText[0]);
      print(splitedText[1]);
      

      【讨论】:

      • Text(item[pos].asr) 的数据在我在移动屏幕上运行时显示为 22:00,您提到了“硬编码”声明的字符串。
      猜你喜欢
      • 1970-01-01
      • 2019-10-16
      • 2021-07-20
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2021-02-03
      • 2021-02-05
      相关资源
      最近更新 更多