简体   繁体   English

使用带有变量的延迟库

[英]Using deferred libraries with variables

Sorry in advance I am a beginner and my English is approximate.提前抱歉,我是初学者,我的英语是近似的。

I don't know if what I'm doing is correct or not so sorry (again) if this sounds stupid to you.如果这听起来很愚蠢,我不知道我所做的是否正确或不那么抱歉(再次)。 I try to recover the contents of certain files only when I need them.我只在需要时尝试恢复某些文件的内容。 I think it's good for optimizing my dart/flutter app and reducing loading times.我认为这有助于优化我的 dart/flutter 应用程序并减少加载时间。

So So I have a list of files coded like this :所以我有一个这样编码的文件列表:

  • N101aze.dart which contains a Map called N101aze N101aze.dart 包含一个名为 N101aze 的地图
  • N101qsd.dart which contains a List called N101qsd N101qsd.dart 包含一个名为 N101qsd 的列表
  • N101wxc.dart which contains a List called N101wxc N101wxc.dart 包含一个名为 N101wxc 的列表
  • ... ...

In the same way I have :以同样的方式我有:

  • N102aze.dart which contains a Map called N102aze N102aze.dart 包含一个名为 N102aze 的地图
  • N102qsd.dart which contains a List called N102qsd N102qsd.dart 包含一个名为 N102qsd 的列表
  • N102wxc.dart which contains a List called N102wxc N102wxc.dart 包含一个名为 N102wxc 的列表
  • ... ...

etc... ETC...

Each time I have another file called and coded like that :每次我有另一个文件这样调用和编码时:

N101.dart N101.dart

export "path[...]/N101aze.dart";
export "path[...]/N101qsd.dart";
export "path[...]/N101wxc.dart";
...

Now in a file I want to make a Deferred loading like that :现在在一个文件中,我想像这样进行延迟加载:

import "path[...]/N101.dart" deferred as N101; //I know lowercase is better

I load the library when I want by using我在需要时通过使用加载库

greet() async {
  await N101.loadLibrary();
}

and later I can call my maps and lists by using后来我可以通过使用来调用我的地图和列表

N101.N101aze
N101.N101qsd

It's working perfectly when I use the names listed above.当我使用上面列出的名称时,它工作得很好。

Now, what I really want is to use a key (called kkey here) to call the library I want when I call onPressed on ElevatedButton :现在,当我在 ElevatedButton 上调用 onPressed 时,我真正想要的是使用一个键(这里称为 kkey)来调用我想要的库:

import 'package:flutter/material.dart';
import 'package:my_app/Models/SetColor.dart';

import "path[...]/N101.dart" deferred as N101; //I know lowercase is better
import "path[...]/N102.dart" deferred as N102;
import "path[...]/N103.dart" deferred as N103;
...

class CustomButton extends StatefulWidget {
  const CustomButton({
    required this.kkey,
  }) : super(key: key);
  final String kkey;

  @override
  State<CustomButton> createState() => _CustomButtonState();
}

class _CustomButtonState extends State<CustomButton> {

  Future<void> greet(thekey) async {
    await thekey.loadLibrary();
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Text(
        widget.kkey
      ),
      onPressed: () async {
        await greet(widget.kkey); //it's not working beacause kkey type is String
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => LoadingParts(
              part1: // here I want N101.N101aze when kkey="N101" so I want something like kkey.kkeyaze 
              part2: // here I want N101.N101qsd when kkey="N101" so I want something like kkey.kkeyqsd

...

Note :笔记 :

  • I "simplified" the code to keep only the essentials我“简化”了代码以只保留必需品
  • part1 is a Map and part2 is a List第 1 部分是地图,第 2 部分是列表

For the greet function, I think I understand it doesn't work because N101, N102, N103... have no type and my kkey is the type of String but I don't know what is possible to do...对于 greet 函数,我想我理解它不起作用,因为 N101、N102、N103 ......没有类型,我的 kkey 是 String 的类型,但我不知道可以做什么......

To invoke my maps and my lists, I have no idea what to do... Especially when I want to concatenate kkey with aze (for example).要调用我的地图和列表,我不知道该怎么做......尤其是当我想将 kkey 与 aze 连接时(例如)。

I know I can use Switch Case Statement which looks at all characters from kkey to return, for example "N101.loadlibrary()" and "N101.N101.aze" but the number of "Switch" and "Case" is too big to be worth it and the number of possibilities may increase as the application grows.我知道我可以使用 Switch Case 语句查看 kkey 中的所有字符以返回,例如“N101.loadlibrary()”和“N101.N101.aze”,但“Switch”和“Case”的数量太大而无法返回值得,并且随着应用程序的增长,可能性的数量可能会增加。

I hope you will understand my problem and I hope it's not too stupid, I'm learning^^.我希望你能理解我的问题,我希望它不会太愚蠢,我正在学习^^。

Thank you for your attention!感谢您的关注! Bye!再见!

Unfortunately, there is no way to do it dynamically this way ie getting a declaration by name.不幸的是,没有办法以这种方式动态地进行,即按名称获取声明。 Although Dart is a static language that supports reflection, flutter does not by default.虽然 Dart 是支持反射的静态语言,但 Flutter 默认不支持。 In Flutter the mirrors package is disabled in favour of static optimization.在 Flutter 中, mirrors包被禁用以支持静态优化。 There is also reflectable but it has limitations where it's not possible to reflect a module for example.也有可reflectable的,但它有一些限制,例如无法反射模块。

So, the way to fix it is to use static code too to load the deferred libs like the following.因此,修复它的方法是也使用静态代码来加载延迟库,如下所示。

On each NXXX.dart file, where XXX is 101, 102, 103 etc., declare the same map.在每个NXXX.dart文件上,其中XXX为 101、102、103 等,声明相同的映射。 For example, in N101.dart it's going to be like this:例如,在N101.dart中它将是这样的:

// Create a map with all structures needed
final nmap = {
  'aze': N101aze,
  'qsd': N101qsd,
  'wxc': N101wxc,
};

And then the usage is going to be like this:然后用法将是这样的:

class CustomButton extends StatefulWidget {
  const CustomButton({
    Key? key,
    required this.kkey,
  }) : super(key: key);
  final String kkey;

  @override
  State<CustomButton> createState() => _CustomButtonState();
}

class _CustomButtonState extends State<CustomButton> {
  Future<Map<String, Object>> greet(String thekey) async {
    // Load the deferred module and return the structures
    switch (thekey) {
      case 'N101':
        await N101.loadLibrary();
        return N101.nmap;
      case 'N102':
        await N102.loadLibrary();
        return N102.nmap;
      case 'N103':
        await N103.loadLibrary();
        return N103.nmap;
      default:
        assert(false);
        throw Exception('$thekey not mapped');
    }
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Text(widget.kkey),
      onPressed: () async {
        // Get the common `nmap` from `greet` by `kkey`
        final nmap = await greet(widget.kkey);
        if (mounted) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => LoadingParts(
                // Use the `nmap` by name
                part1: nmap['aze']! as Map,
                part2: nmap['qsd']! as List,
                part3: nmap['wxc']! as List,
              ),
            ),
          );
        }
      },
    );
  }
}

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

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