如何在迭代期间从列表中减去字符串值?扑

Posted

技术标签:

【中文标题】如何在迭代期间从列表中减去字符串值?扑【英文标题】:How to subtract a String Value from a List during Iteration? Flutter 【发布时间】:2021-08-26 02:55:33 【问题描述】:

我有一个包含小部件构造函数的列表,这些构造函数是字符串,用于不同的类。该列表由 39 个标签组成,这 39 个标签有不同的文本,有些页面只有 7 个标签,如何在迭代过程中只显示该类显示为字符串的标签数量?

List<String> tests;
// @override
// void initState() 
// super.initState();     tests = [widget.label00, widget.label0]; 

  @override
  void initState() 
    super.initState();
    tests = [
      widget.label00,
      widget.label0,
      widget.label1,
      widget.label2,
      widget.label3,
      widget.label4,
      widget.label5,
      widget.label6,
      widget.label7,
      widget.label8,
      widget.label9,
      widget.label10,
      widget.label11,
      widget.label12,
      widget.label13,
      widget.label14,
      widget.label15,
      widget.label16,
      widget.label17,
      widget.label18,
      widget.label19,
      widget.label20,
      widget.label21,
      widget.label22,
      widget.label23,
      widget.label24,
      widget.label25,
      widget.label26,
      widget.label27,
      widget.label28,
      widget.label29,
      widget.label30,
      widget.label31,
      widget.label32,
      widget.label33,
      widget.label34,
      widget.label35,
      widget.label36,
      widget.label37,
      widget.label38,
      widget.label39
    ];

    // List<ReusableLarge> translationTextsss = [
    //   ReusableLarge(label00: "test00"),
    //   ReusableLarge(label0: "test0"),
    //   ReusableLarge(label1: 'test1'),
    //   ReusableLarge(label2: 'test2'),
    //   // ),
    // ];

    Timer.periodic(Duration(seconds: 5), (Timer timer) 
      if (widget.label7 == '') 
        setState(() 
          tests.length - 1;
        );

        if (_currentPage < tests.length) 
          _currentPage++;
         else 
          _currentPage = 0;
        

        _pageController.animateToPage(
          _currentPage,
          duration: Duration(milliseconds: 350),
          curve: Curves.easeIn,
        );
      
    );
  

以及显示字符串的类之一: 我该怎么做才能使带有标签的卡片:''不显示?我尝试了tests.length-1,但它显示了一条曲折的线条,我希望tests.length根据该标签是否包含字符串而改变。

label00: 'Surah Fatiha' + '\n' + 'The Opening',
      label0: 'In the name of God, the Lord of Mercy, the Giver of Mercy!',
      label1: 'AYAH 1: Praise belongs to God, Lord of the Worlds,',
      label2: 'AYAH 2: the Lord of Mercy, the Giver of Mercy,',
      label3: 'AYAH 3: Master of the Day of Judgement.',
      label4: 'AYAH 4: It is You we worship; it is You we ask for help.',
      label5: 'AYAH 5: Guide us to the straight path:',
      label6:
          'AYAH 6: the path of those You have blessed, those who incur no anger and who have not gone astray.',
      label7: '',
      label8: 'label8',
      label9: '',
      label10: '',
      label11: '',
      label12: '',
      label13: '',
      label14: '',
      label15: '',
      label16: "",
      label17: '',
      label18: '',
      label19: '',
      label20: '',
      label21: '',
      label22: '',
      label23: '',
      label24: '',
      label25: '',
      label26: '',
      label27: "",
      label28: '',
      label29: '',
      label30: '',
      label31: '',
      label32: '',
      label33: '',
      label34: '',
      label35: '',
      label36: '',
      label37: '',
      label38: '',
      label39: '',

【问题讨论】:

【参考方案1】:

我能够通过一个包含所有小部件循环它们的列表并将其添加到另一个列表来解决这个障碍,这对我正在寻找的东西产生了奇迹: 结果 = [];

for (int i = 0; i < tests.length; i++) 
  if (widget.label00 != '') 
    results.add(widget.label00);
  
  if (widget.label0 != '') 
    results.add(widget.label0);
  
  if (widget.label1 != '') 
    results.add(widget.label1);
  
  if (widget.label2 != '') 
    results.add(widget.label2);
  
  if (widget.label3 != '') 
    results.add(widget.label3);
  
  if (widget.label4 != '') 
    results.add(widget.label4);
  
  if (widget.label5 != '') 
    results.add(widget.label5);
  
  if (widget.label6 != '') 
    results.add(widget.label6);
  
  if (widget.label7 != '') 
    results.add(widget.label7);
  
  if (widget.label8 != '') 
    results.add(widget.label8);
  
  if (widget.label9 != '') 
    results.add(widget.label9);
  
  if (widget.label10 != '') 
    results.add(widget.label10);
  
  if (widget.label11 != '') 
    results.add(widget.label11);
  
  if (widget.label12 != '') 
    results.add(widget.label12);
  
  if (widget.label13 != '') 
    results.add(widget.label13);
  
  if (widget.label14 != '') 
    results.add(widget.label14);
  
  if (widget.label15 != '') 
    results.add(widget.label15);
  
  if (widget.label16 != '') 
    results.add(widget.label16);
  
  if (widget.label17 != '') 
    results.add(widget.label17);
  
  if (widget.label18 != '') 
    results.add(widget.label18);
  
  if (widget.label19 != '') 
    results.add(widget.label19);
  
  if (widget.label20 != '') 
    results.add(widget.label20);
  
  if (widget.label21 != '') 
    results.add(widget.label21);
  
  if (widget.label22 != '') 
    results.add(widget.label22);
  
  if (widget.label23 != '') 
    results.add(widget.label23);
  
  if (widget.label24 != '') 
    results.add(widget.label24);
  
  if (widget.label25 != '') 
    results.add(widget.label25);
  

  if (widget.label26 != '') 
    results.add(widget.label26);
  
  if (widget.label27 != '') 
    results.add(widget.label27);
  
  if (widget.label28 != '') 
    results.add(widget.label28);
  
  if (widget.label29 != '') 
    results.add(widget.label29);
  
  if (widget.label30 != '') 
    results.add(widget.label30);
  
  if (widget.label31 != '') 
    results.add(widget.label31);
  
  if (widget.label32 != '') 
    results.add(widget.label32);
  
  if (widget.label33 != '') 
    results.add(widget.label33);
  
  if (widget.label34 != '') 
    results.add(widget.label34);
  
  if (widget.label35 != '') 
    results.add(widget.label35);
  
  if (widget.label36 != '') 
    results.add(widget.label36);
  
  if (widget.label37 != '') 
    results.add(widget.label37);
  

【讨论】:

以上是关于如何在迭代期间从列表中减去字符串值?扑的主要内容,如果未能解决你的问题,请参考以下文章

在每次迭代期间将新值附加到列表中后,将列表值添加到字典中

如何判断一个值是 Django 模板中的字符串还是列表?

如何从列表 Dart 中删除重复项 |扑

在迭代期间从列表中删除

java - 如何从Java中存储日期的给定字符串字段中减去X天? [复制]

如何将列表链接到 if else?扑