flutter开发桌面程序_在开始使用Flutter开发应用程序之前要了解的5条提示

flutter开发桌面程序

1.始终取消您的流订阅: (1. Always cancel your stream subscription:)

When I started learning Flutter, I did not always think about canceling my stream subscription because that was something I never heard about.

当我开始学习Flutter时,我并不总是考虑取消流订阅,因为那是我从未听说过的事情。

But what I learned is that even if your widget is no longer alive, well, your stream subscription still is! And that can become a major issue when the widget is re-created a certain number of times, leading to possible process overload!

但是我了解到,即使您的小部件不再存在,您的流订阅仍然有效! 当重新创建窗口小部件一定次数时,这可能成为一个主要问题,从而可能导致进程过载!

To prevent that, you could just add a conditional block that checks if the widget is mounted, but that would still result in a useless loss of performance.

为防止这种情况,您可以只添加一个条件块来检查该窗口小部件是否已安装,但这仍然会导致无用的性能损失。

So to prevent that here is how you should manage your stream subscription in Flutter:

因此,为防止这种情况,您应该在Flutter中管理流订阅:

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}


class _MyWidgetState extends State<MyWidget> {
  StreamSubscription _streamSubscription;
  
  int _counter = 0;
  
  @override
  void initState() {
    super.initState();
    
    _streamSubscription = Stream<void>.periodic(Duration(milliseconds: 500)).listen((void _) {
      setState(() {
        _counter++;
      });
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Text(_counter.toString());
  }
  
  @override
  void dispose() {
    super.dispose();
    
    _streamSubscription.cancel();
  }
}

But the easiest way can sometimes be to just use a StreamBuilder. It is a very powerful widget that will manage the state of its child depending on the stream provided! If you want to know more I would suggest that you read this article from Manzurur Rahman Khan.

但是最简单的方法有时可能是只使用StreamBuilder。 这是一个非常强大的小部件,它将根据提供的流来管理其子状态! 如果您想了解更多信息,建议您阅读Manzurur Ra​​hman Khan的这篇文章。

2.是否要从应用程序中的任何位置访问当前的导航器状态? 方法如下: (2. Do you want to access the current Navigator state from anywhere in your app? Here is how:)

Let’s say that whenever the user receives a certain type of notification, you want to push a view corresponding to the notification.

假设每当用户收到某种通知时,您都希望推送与该通知相对应的视图。

The problem would be that you don’t have access to any BuildContext object which will result in no way to access the current instance of the Navigator.

问题在于您无权访问任何BuildContext对象,这将导致无法访问导航器的当前实例。

The solution is to create an instance of GlobalKey and store it in a global variable anywhere you want. Then simply specify that the navigator key parameter of your MaterialApp instance should be this variable.

解决方案是创建GlobalKey的实例,并将其存储在全局变量中所需的任何位置。 然后,只需指定MaterialApp实例的导航键参数为该变量即可。

Now you can simply access the current Navigator state by using the GlobalKey!

现在,您只需使用GlobalKey即可访问当前的导航器状态!

3.始终在无状态小部件中使用最终变量。 (3. Always Use Final Variables In Your Stateless widgets.)

It can sometimes be tempting to instantiate non-final variables in stateless widgets but that’s something you should always avoid because stateless widgets are immutable which means that they are not supposed to change even a bit!

在无状态小部件中实例化非最终变量有时可能很诱人,但这是您应始终避免的事情,因为无状态小部件是不可变的,这意味着它们甚至不应改变!

If you need a non-final variable, consider using a Stateful widget instead!

如果需要非最终变量,请考虑使用有状态的小部件!

4. Flutter可能很冗长,不要偷懒,想想接下来会发生什么! (4. Flutter can be quite verbose, don’t be lazy, think of what’s ahead!)

If you already work with Flutter in your daily life, you probably know how Flutter can be extremely verbose, making you re-write the same code over and over!

如果您已经在日常生活中使用Flutter,那么您可能知道Flutter可能会非常冗长,从而使您一遍又一遍地重写相同的代码!

But, instead of being lazy and just copy-pasting your code, you should create reusable widgets! That will save you a ton of time and make your code more maintainable!

但是,您应该创建可重用的小部件,而不是懒惰并只复制粘贴您的代码! 这样可以节省大量时间,并使代码更易于维护!

If you don’t see how you could do that, you can check this article!

如果您看不到该怎么做,可以查看本文

5.不能使用const构造函数 (5. Use const Constructors when you cant)

Did you even know that Dart had a const constructor? A const constructor is pretty useful in terms of performance and the reason for it is simple!

您甚至不知道Dart具有const构造函数吗? const构造函数在性能方面非常有用,其原因很简单!

Let’s say that you create a simple text widget that displays ‘Hello World’. You want to display this text in different places of your app but the thing is that you don’t want to instantiate it more than once.

假设您创建了一个显示“ Hello World”的简单文本小部件。 您想在应用程序的不同位置显示此文本,但事实是您不想多次实例化它。

The solution that may come to your mind if you never heard about const constructors would be to define a global variable containing the text widget. But this solution is not really… esthetic.

如果您从未听说过const构造函数 ,可能想到的解决方案是定义一个包含文本小部件的全局变量。 但是这种解决方案并不是真的……美观。

As you probably guessed it, the good solution here would be to use a const constructor! A const constructor will store the instance and returns it wherever you create the same instance of your class with a const constructor!

您可能已经猜到了,这里的解决方案是使用const构造函数! const构造函数将存储该实例,并在您使用const构造函数创建类的相同实例时将其返回!

So basically, you will only have one instance of your text widget if you always use it with a const constructor, no matter how many times you ‘instantiate’ it in your code.

因此,基本上,如果您始终将其与const构造函数一起使用,则您将只有一个文本小部件实例,无论您在代码中“实例化”多少次。

结束 (The End)

I hope you liked this article :)

希望您喜欢这篇文章:)

https://www.twitter.com/FlutterComm

https://www.twitter.com/FlutterComm

翻译自: https://medium.com/flutter-community/5-tips-to-know-before-you-start-developing-your-app-with-flutter-50771507dae0

flutter开发桌面程序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值