[英]What is the difference between the codes for importing and calling Class from another dart.file (Flutter)
1) As you can see I'm practicing flutter bloc pattern. 1)如您所见,我正在练习 flutter bloc 模式。 There are lots of imports here.这里有很多进口。 What is difference between them?它们之间有什么区别? ( package:bloc/bloc.dart)(bloc.dart)(package:test1/bloc.dart) test1 is my project name. ( package:bloc/bloc.dart)(bloc.dart)(package:test1/bloc.dart) test1 是我的项目名称。
2) And I try to call Bloc Class which is in the bloc.dart file. 2)我尝试调用 bloc.dart 文件中的 Bloc Class。 But it says Bloc() isn't a function.但它说 Bloc() 不是 function。 What should I do?我应该怎么办?
There is no pratical difference between packages imports and relative imports, but packages imports are prefered for code legibility and organization.包导入和相对导入之间没有实际区别,但包导入更适合代码易读性和组织。 Also, relative imports tend to be a pain when you change a file from place and have to rename all the imports.此外,当您从位置更改文件并且必须重命名所有导入时,相对导入往往会很痛苦。
You "can" use both kinds of imports for the same file cause Dart treats them as 2 different namespaces but that will probably lead to bugs, probably that is what is causing your error.您“可以”对同一个文件使用两种导入,因为 Dart 将它们视为 2 个不同的命名空间,但这可能会导致错误,这可能就是导致错误的原因。
Regarding the BLoC pattern, it seems that you are using a the pure core of BLoC, I recommend you to study the implementation of flutter_bloc (which is made by the BLoC team).关于 BLoC 模式,看来你使用的是 BLoC 的纯核心,我建议你研究一下flutter_bloc的实现(由 BLoC 团队制作)。 It's the way that BLoC is used more often.这是更经常使用 BLoC 的方式。 There is also a great documentation by the BLoC team in here . BLoC 团队在这里也有一个很棒的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.