如何在dart flutter中访问数组中的数组元素?

kqlmhetl  于 2023-07-31  发布在  Flutter
关注(0)|答案(1)|浏览(153)

我试图创建一个Flutter应用程序,从语音中获取命令,然后根据特定的命令,它将打印特定的文本。为此,我创建了一个类Command,它有一个commands数组,其中还包含3个数组,如下所示:

class Command {
  static final commands = [
    activity,
    face,
    object,
  ];

  static const activity = ['activity,is person using, giving, taking, waving'],
      face = ['standing, which teacher, looking, coming'],
      object = ['where is, is there'];
}

字符串
问题是我不知道如何访问Activity、Face和Object数组。错误发生在这一行:
terms:Command.命令,

body: SingleChildScrollView(
          reverse: true,
          child: Padding(
            padding: const EdgeInsets.all(20.0).copyWith(bottom: 140),
            child: SubstringHighlight(
              text: textSample,
              terms: Command.commands,
              textStyle: const TextStyle(
                color: Colors.teal,
                fontSize: 30,
              ),
              textStyleHighlight: const TextStyle(
                  color: Colors.blue,
                  fontSize: 30,
                  fontWeight: FontWeight.bold),
            ),
          )),


我也试过其他方法,但都出错了。
terms:Command.commands[],
请告诉我如何访问要匹配的数组中的完整数组以显示结果。

xmjla07d

xmjla07d1#

问题是我不知道如何访问Activity、Face和Object数组

class Command {
  var commands = [
    activity,
    face,
    object,
  ];

  static const activity = [
        'activity,is person using, giving, taking, waving',
        'second value in activity'
      ],
      face = ['standing, which teacher, looking, coming'],
      object = ['where is, is there'];
}

字符串
要访问这些数据,您必须这样做:

print(Command().commands);
// Print all value inside commands variable [[activity,is person using, giving, taking, waving], [standing, which teacher, looking, coming], [where is, is there]]

print(Command().commands[0]);
// Print all activity variable: [activity,is person using, giving, taking, waving]

print(Command().commands[0][1]);
// Print value inside activity variable by index, 'second value in activity'

相关问题