【问题标题】:Display array items one at a time in Text Widget在文本小部件中一次显示一个数组项
【发布时间】:2021-06-07 14:17:10
【问题描述】:

我正在使用这个数组在 Text Widget 中使用它的值:

var heartData = ['76','76','80', '76','82','75','73','75'];

现在我想在文本小部件中一一显示这个数组数据:

StreamBuilder<List<int>>(
                                    stream: stream,
                                    initialData: lastValue,
                                    builder: (BuildContext context,
                                        AsyncSnapshot<List<int>> snapshot) {
                                      if (snapshot.connectionState ==
                                          ConnectionState.active) {
                                        var currentVal =
                                            snapshot.data.toString();
                                        int currentValue = int.parse(
                                            currentVal.substring(
                                                1, currentVal.length - 1),
                                            onError: (source) => -1);
                                        print("String data $currentValue");
                                        if (currentValue == 2) {
                                      
                                          return FutureBuilder(
                                              future: Future.delayed(
                                                  const Duration(seconds: 0),
                                                  () async {
    
                                                Seziurealert();
                                              }),
                                              builder: (context, snapshot) {
             // I want to display array data here one by one
                                                return Text(
                                                  'heartData[i]',
                                                  style: TextStyle(
                                                    fontFamily:
                                                        'SF Pro Display',
                                                    fontSize: 19,
                                                    color:
                                                        const Color(0xffffffff),
                                                    fontWeight: FontWeight.w500,
                                                    height: 1.4736842105263157,
                                                  ),
                                                );
                                              });
                                        } else if (currentValue == 0) {
                                          return Text(
                                            'Device not calibrated',
                                            style: TextStyle(
                                              fontFamily: 'SF Pro Display',
                                              fontSize: 19,
                                              color: const Color(0xffffffff),
                                              fontWeight: FontWeight.w500,
                                              height: 1.4736842105263157,
                                            ),
                                          );
                                        } else if (currentValue == 1) {
    // I want to display array data here one by one
                                          return Text(
                                            'heartData[i]',
                                            style: TextStyle(
                                              fontFamily: 'SF Pro Display',
                                              fontSize: 19,
                                              color: const Color(0xffffffff),
                                              fontWeight: FontWeight.w500,
                                              height: 1.4736842105263157,
                                            ),
                                          );
                                        }
    // I want to display array data here one by one
                                        return Text(
                                          'heartData[i]',
                                          style: TextStyle(
                                            fontFamily: 'SF Pro Display',
                                            fontSize: 19,
                                            color: const Color(0xffffffff),
                                            fontWeight: FontWeight.w500,
                                            height: 1.4736842105263157,
                                          ),
                                        );
                                      } else {
                                        return Text(
                                          'Check the stream',
                                          style: TextStyle(
                                            fontFamily: 'SF Pro Display',
                                            fontSize: 19,
                                            color: const Color(0xffffffff),
                                            fontWeight: FontWeight.w500,
                                            height: 1.4736842105263157,
                                          ),
                                        );
                                      }
                                    },
                                  ),

请帮助我如何在我的文本小部件中使用这个数组?我在 Stream Builder 中使用此文本,并且在满足条件时显示文本小部件 我想一一获取这些数据,而不是列表形式。我怎样才能做到这一点? 提前谢谢!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    数组中有一个名为join的方法,你可以调用它:

     return Text(
      heartData.join(),
      style: TextStyle (
        fontFamily: 'SF Pro Display',
        fontSize: 19,
        color: const Color(0xffffffff),
        fontWeight: FontWeight.w500,
        height: 1.4736842105263157,
      ),
    );
    

    结果将是 7676807682757375

    您也可以将分隔符传递给此函数heartData.join(separator: "-")

    结果将是 76-76-80-76-82-75-73-75

    【讨论】:

    • 但我不希望整个数组同时出现,我希望前 76 应该显示为 Text 然后它会继续,数组中的下一个数字,即 76 应该被显示,依此类推,直到数组完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2022-06-23
    • 1970-01-01
    相关资源
    最近更新 更多