简体   繁体   English

Dart中如何使用头文件

[英]How to use Header files in Dart

I have a list of about 100K entries that have this format:我有一个大约 100K 条目的列表,这些条目具有以下格式:

final codes = <Results>[Results('ccc857'),
Results('dea105'),
Results('fad975'),
Results('abf307'),
Results('faf995'),...(continuing for 100K lines)

If I place these directly into main.dart android studio hangs.如果我将这些直接放入 main.dart android studio 挂起。

I would like to have this as a header file abc.h but I don't know of a Dart equivalent.我想将其作为头文件 abc.h,但我不知道 Dart 等效项。

Thank you.谢谢你。

Dart does not have any notion of header files. Dart 没有任何头文件的概念。

You are asking to create 100K objects, each containing ~1 string.您要求创建 100K 个对象,每个对象包含 ~1 个字符串。 The compiler also needs 100K nodes to represent that source code, so that's a lot of objects.编译器还需要 10 万个节点来表示该源代码,因此有很多对象。 And a lot of code (your program will contain code for 100K different calls to the Result constructor).以及大量代码(您的程序将包含对Result构造函数的 100K 次不同调用的代码)。

Consider instead doing something like:考虑改为执行以下操作:

final codes = _createCodes();
static List<Result> _createCodes() {
  const strings = ["dea105", "fad975", "abf307", "faf995",
  "......", .... 
  "......"];
  return [for (var string in strings) Result(string)];
}

That is, don't repeat the entire expression 100K times, use a loop, and only have the part that differs between the iterations as separate code.也就是说,不要将整个表达式重复 100K 次,使用循环,并且只将迭代之间不同的部分作为单独的代码。 That should reduce the overhead of your exceedingly large code.这应该会减少您的超大代码的开销。

No promises it won't hang anyway, with a 100K element list literal.没有承诺它无论如何都不会挂起,带有 100K 元素列表文字。 Consider if you can load those strings from a file at run-time instead.考虑是否可以在运行时从文件加载这些字符串。 Or maybe put them into one string literal:或者也许将它们放入一个字符串文字中:

final codes = _createCodes();
static List<Result> _createCodes() {
  const strings = "dea105fad975abf307faf995...."
  "...........",  
  ...
  ".......,...";
  return [for (var i = 0; i < strings.length; i++) 
      Result(strings.substring(i, i + 6))
  ];
}

(That does create an awful lot of strings at runtime, but only one at compile-time. One 600K character string literal. That might have its own problems.) (这确实在运行时创建了大量字符串,但在编译时只有一个。一个 600K 字符串文字。这可能有其自身的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM