【问题标题】:How To Random Select A String From List After Tabbing Button标签按钮后如何从列表中随机选择一个字符串
【发布时间】:2021-08-11 05:24:22
【问题描述】:

我想按一个按钮并从我的列表中选择一个随机字符串以显示在屏幕上的某个位置。

当前,构建器中的 convoTopic 变量正在运行错误。

感谢任何帮助!

以下是我截断的代码:


final List<String> ConvoTopics = [
'blah blah',
'black sheep',
'balh bath'
];

class ConvoPage extends StatefulWidget {

  @override
  _ConvoPageState createState() => _ConvoPageState();
}

class _ConvoPageState extends State<ConvoPage> 

  @override
  Widget build(BuildContext context) {
    void generateConvoTopic() {
      final _random = Random();
      var convoTopic = ConvoTopics[_random.nextInt(ConvoTopics.length)];
      print(convoTopic);
    }

    return Scaffold(
      backgroundColor: Color(0xff04072E),
      appBar: AppBar(
        title: Text('Convo', style: TextStyle(color: Colors.white)),
        backgroundColor: Color(0xff04072E),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(children: <Widget>[
            Container(
                child: Align(
                    alignment: Alignment.center,
                    child: Text(convoTopic,
                    ),
// where randomized string appears
              ),
            ),
            ElevatedButton(
            
              onPressed: () async {
                generateConvoTopic();
              },
    // button's function is to randomize convo topic
              child: Container(
                child: Text('Get Convo'),
              ),
            ),
:
:
:

【问题讨论】:

  • _ConvoPageState中定义convoTopic的初始值,然后当你生成一个新的convoTopic时你必须调用setState来重建build方法。

标签: string list flutter random


【解决方案1】:

你的方法大部分是正确的。

你错过的三件小事是

  1. 您应该在您的State 类中定义您的generateConvoTopic,而不是build 方法。

  2. 您的convoTopic 应该是State 类变量。您当前正在 function 方法内的 build 内定义它。所以它只能在你的function 中访问,但你需要更新它并阅读它,所以它将进入State 类。

  3. 当您想更新State 类的任何变量时,您应该调用setState。在你的情况下,convoTopic 在你执行第 2 步之后。

  4. 列表项

;

final List<String> ConvoTopics = ['blah blah', 'black sheep', 'balh bath']
class ConvoPage extends StatefulWidget {
  @override
  _ConvoPageState createState() => _ConvoPageState();
}

class _ConvoPageState extends State<ConvoPage> {
  String convoTopic;

  void generateConvoTopic() {
    setState(() {
      convoTopic = ConvoTopics[Random().nextInt(ConvoTopics.length)];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff04072E),
      appBar: AppBar(
        title: Text('Convo', style: TextStyle(color: Colors.white)),
        backgroundColor: Color(0xff04072E),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
            child: Column(children: <Widget>[
              Container(
                child: Align(
                  alignment: Alignment.center,
                  child: Text(convoTopic),
                ),
              ),
              ElevatedButton(
                onPressed: () async {
                  generateConvoTopic();
                },
                child: Container(
                  child: Text('Get Convo'),
                ),
              ),
    ]))));
  }
}

【讨论】:

  • 谢谢尼桑。知道了! Nisanth,你知道如何解决这个问题吗? stackoverflow.com/questions/67656339/… 谢谢!
  • 很高兴有帮助:)。检查另一个。
  • 好像已经有答案了,看看吧
  • 嗨@VegetaSaiyan。是的,我是一名全栈开发人员,但我一个月前才开始学习 Flutter。我曾经在 React Native 工作。但我来自印度,上周也换了工作。所以我现在不寻求改变。但在未来的某个时候,也许?您可以随时通过linkedin.com/in/reddynishanth与我联系
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
相关资源
最近更新 更多