简体   繁体   English

Flutter 中全局变量的最佳实践

[英]Best practices for globals in flutter

I'm building an app that has text to speech.我正在构建一个具有文本到语音的应用程序。 It's possible to change the voice and set speed and such.可以改变声音和设置速度等。 I'm using a standard TTS from the public dev.我正在使用来自公共开发人员的标准 TTS。

My question is, how to initialize this and use it throughout the app without passing recreating it everywhere since the object rarely will change.我的问题是,如何初始化它并在整个应用程序中使用它,而无需在任何地方重新创建它,因为对象很少会改变。

Once it is set up, I just want to have it speak.一旦设置好,我只想让它说话。

It's certainly not impossible to recreate it every time I need it.每次我需要它时都重新创建它当然不是不可能的。

I'm looking for best practices.我正在寻找最佳实践。

Just have a class that holds all the information with an initialize method:只需要一个包含所有信息的类和一个初始化方法:

class Constants {

  static bool firstVar;
  static String secondVar;

  static void initialize() {
    firstVar = true;
    secondVar = "";
  }
}

// then call it in your main method before building the first widget

Constants.initialize();

In this case, you need to use a state management architecture.在这种情况下,您需要使用状态管理架构。

There are a good number of them to choose from.有很多可供选择。 There are many articles and videos that explain how to use them.有许多文章和视频解释了如何使用它们。 One thing they have in common is just what you're looking for.他们有一个共同点就是你在寻找什么。 The ability to provide state at an app-wide level without reconfiguring in each widget.无需在每个小部件中重新配置即可在应用程序范围内提供状态的能力。

Check out the Flutter Documentation here . 在此处查看 Flutter 文档 Follow through the next pages and you'll have a good general overview of state management.浏览下一页,您将对状态管理有一个很好的总体概述。 Available options include, Provider, Riverpod, Redux, InheritedWidget, Stacked , etc. (Many of them actually). 可用的选项包括 Provider、Riverpod、Redux、InheritedWidget、 Stacked等(实际上有很多)。

For example, with Provider, you initialise the TTS in some TTS StateProvider (just an example).例如,使用 Provider,您可以在某个 TTS StateProvider 中初始化 TTS(只是一个示例)。 Then after wrapping your topmost MaterialApp in a ProviderScope, you can easily use the TTS in any widget as follows:然后将最顶层的 MaterialApp 包装在 ProviderScope 中后,您可以轻松地在任何小部件中使用 TTS,如下所示:

final tts = Provider.of<TTSProvider>(context).tts;
tts.doWhateverYouWant();

Another advantage of these architectures is that a good number of them permit good separation of concerns and dependency injection (or inversion of control) .这些架构的另一个优点是它们中的大量允许关注点和依赖注入(或控制反转)的良好分离

In simpler terms, separation of concerns give you have the ability to write UI specific code separate from logic specific code.简单来说,关注点分离使您能够将特定于 UI 的代码与特定于逻辑的代码分开编写。 This way, if you want to debug, or change the packages or APIs you're using, you'll do them easily from one place (without fear of damaging the codebase).这样,如果您想调试或更改您正在使用的包或 API,您可以从一个地方轻松完成(不必担心损坏代码库)。 Besides it promotes clean code too.此外,它也促进了干净的代码。

Dependency Injection involves the use of services or something similar to obtain what widgets need to work without the widgets configuring the services themselves.依赖注入涉及使用服务或类似的东西来获取小部件需要工作的内容,而无需小部件自己配置服务。

As you explore, you'll also notice that some architectures like Stacked permit you to manage app-wide state without the BuildContext.随着您的探索,您还会注意到某些架构(如 Stacked)允许您在没有 BuildContext 的情况下管理应用程序范围的状态。 For another example for TTS, with Stacked Architecture, you could have:对于 TTS 的另一个示例,使用堆叠架构,您可以:

final tts = locator<TTSService>().tts;
tts.doWhateverYouWant();

This pattern is useful because, in StatelessWidgets, the BuildContext is available only within the build method and you might need to use stuff outside it.这种模式很有用,因为在 StatelessWidgets 中,BuildContext 仅在 build 方法中可用,您可能需要使用它之外的东西。 In addition to obtaining the TTS anywhere in the Flutter code, you could do other things like BottomSheet, Navigation, Toast, etc. without BuildContext.除了在 Flutter 代码中的任意位置获取 TTS 之外,您还可以在没有 BuildContext 的情况下执行其他操作,例如 BottomSheet、Navigation、Toast 等。

Furthermore, you'll notice that you will tend to use more StatelessWidgets as widgets no longer keep state for you.此外,您会注意到您将倾向于使用更多的无状态小部件,因为小部件不再为您保留状态。

On a final note, don't worry about performance.最后一点,不要担心性能。 Flutter in itself is efficient and handles everything properly. Flutter 本身是高效的,并且可以正确处理所有内容。

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

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