简体   繁体   English

从另一个 dart.file (Flutter) 导入和调用 Class 的代码有什么区别

[英]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?我应该怎么办?

also my bloc.dart file:还有我的 bloc.dart 文件: 在此处输入图像描述

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.

相关问题 我想从同一个 dart.file 中的另一个 class 中的基于 function 的小部件中调用一个值到文本小部件中 - I wanna call a value from function based widget from another class in the same dart.file into a Text widget Dart Flutter 导入和扩展的区别是什么? - Dart Flutter What is the Difference Between Import and Extends? Flutter/Dart:库和文件有什么区别? - Flutter/Dart: What is the difference between a library and a flile? 在Dart中导入整个文件与只导入带show的类之间有什么区别? - Any difference between importing whole file and importing only class with show in Dart? 这两种软件包在Dart语言中的导入方式有什么区别? - What is the difference between these two packages importing ways in Dart language? Flutter/Dart - () {} 和 () => {} 之间的区别 - Flutter/Dart - Difference between () {} and () => {} Flutter、Dart - 如何使用另一个文件中的类属性 - Flutter, Dart - How to use a class property from another file EdgeInsetsGeometry Class EdgeInsets Class 在 Flutter Z35900D987289A83AF1011D18A9FZ7C3 之间的区别 - Difference between EdgeInsetsGeometry Class EdgeInsets Class in Flutter Dart? 从 main.dart 调用另一个 dart 文件中的 dart 方法 - Calling a dart method in another dart file from main.dart 从另一个 dart 文件调用 function - Calling a function from another dart file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM