问题描述
有什么方法可以让我在某个服务器上存储多个 dart 文件,并在运行时检索其中的任何一个文件,从而使 Flutter 能够从它接收到的文件中构建特定的小部件?
Is there any by which I can store multiple dart files on some server and retrieve any one of those file during run-time in such a way that Flutter is able to build a particular widget from the file it receives?
推荐答案
你不能动态加载 dart 文件或者创建新的类,不行.
You cannot dynamically load dart files or create new classes, no.
另一方面,小部件树是在运行时创建的,小部件本质上是可组合的.所以完全有可能制作一个将一些数据反序列化为小部件树的函数.
On the other hand, the widget tree is created at runtime, and widgets are composable by nature. So it is totally possible to make a function that deserialize some data into a widget tree.
例如,我们可以将一个小部件树编写为 xml/yaml/whatever 之类的:
We could, for example, write a widget tree as an xml/yaml/whatever like so:
type: Row
children:
- type: Container
color: red
child:
- type: Text
0: hello world
并有一个函数将其反序列化为:
And have a function deserialize it into:
Row(
children: [
Container(
color: Colors.red,
child: Text('hello world'),
),
],
),
这篇关于Flutter 是否能够在运行时动态加载和构建小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!