当前位置:网站首页>Flutter common development problems

Flutter common development problems

2022-06-24 05:11:00 nuts

“ This paper mainly introduces Flutter Common development problems ”

Flutter Using a completely new approach , You can use widgets Instead of Views .Android Medium View It is mainly an element of the layout , But in Flutter in ,Widget Almost everything . Everything from buttons to layout structures are widgets . The advantage here is that it can Customization . Imagine Android One of the buttons in . It has attributes such as text , Allows you to add text to the button . however Flutter The button in does not take the title as a string , It's another widget . It means ** You can have text inside the button 、 Images 、 Icons and almost anything you can imagine ,** Without breaking the layout restrictions . This also makes it very easy for you to make custom widgets , And in the Android It is quite difficult to make custom views in .

Isn't it easier to drag and drop than to make a layout in your code ?

in some ways , Such is the case . however Flutter Many people in the community prefer the code approach , But that doesn't mean you can't drag . If you absolutely love dragging , that Flutter Studio It is a great resource I recommend , It can help you generate layouts by dragging and dropping . This is a tool that impresses me , I really want to see how it develops .

link :https : //flutterstudio.app

Flutter Whether it works like a browser ?/ It is based on WebView What's the difference between our applications ?

Simply answer this question : You are WebView Code written by a running application must go through multiple layers before it can be finally executed . essentially ,Flutter Compile to native ARM Code to execute on both platforms , So that stride across .“ blend ” The application is slow 、 slow , And it looks different from the platform they run on .Flutter Applications run much faster than their hybrid applications . Besides , Using plug-ins to access native components and sensors is more important than using a that cannot take full advantage of its platform WebView More easily .

Why? Flutter There are Android and iOS Folder ?

Flutter The project mainly includes 3 A folder :lib、android and ios.'lib' Responsible for handling your Dart file .Android and iOS Folders exist to actually build applications on their respective platforms , And run on it Dart file . They also help you add permissions and platform - specific functionality to your projects . When you run Flutter Project time , It will be built according to the running simulator or device , Use the folder to Gradle or XCode structure . In short , These folders are the entire application , They are Flutter The running of the code lays the foundation .

Why my Flutter So big ?

If you have run Flutter Applications , You will know it fast . Extremely fast . How it does it ? When building an application , It doesn't just use specific resources , It is Virtually all resources . Why does this help ? Because if I change an icon from one to another , You do not have to completely rebuild the application . This is it. Flutter debugging The reason for building so large . When creating a release , Only the required resources will be obtained , And get the size we're more used to .Flutter Applications will still be better than Android The application is larger , But it's quite small , and Flutter The team has been looking for ways to reduce the size of the application .

If I'm new to programming , I want to start with the development of mobile terminals , It should be from Flutter Do you want to start ?

There are more two-part answers .

  1. Flutter Perfect for writing code , And the code on the same page is better than Android or iOS There are far fewer applications . So for most applications , I don't think there will be a big problem .
  2. One thing you need to remember is Flutter It also depends on Android and iOS project , At a minimum, you need to be familiar with the project structure . If you want to write any native code , You definitely need experience on either or both platforms .

My personal opinion is to study for oneortwo months first Android/iOS, And then from Flutter Start .

What is? package And plug-ins ?

package Allow you to Add new widgets or features Import your application .package There is a small difference between and plug-ins . The package is usually purely for Dart New components or code written , The plug-in can use native code to provide more functions on the device side . Usually in DartPub On , Both packages and plug-ins are called packages , The difference is only explicitly mentioned when creating a new package .

What is? pubspec.yaml file , What does it do ?

Pubspec.yaml Allows you to define the packages that your application depends on , Declare your assets , Such as images 、 Audio 、 Video etc. . It also allows you to set constraints for your application . about Android For developers , This is roughly similar to build.gradle file , But the difference between the two is also obvious .

Why the first Flutter Application building takes so long ?

First build Flutter Application time , Will build specific On The equipment APK or IPA file . therefore , Use Gradle and XCode Building files takes time . The next time you restart or hot load the application ,Flutter Basically patch changes on top of existing applications , So as to provide extremely fast refresh .

Be careful : Changes made by hot reload or restart are not saved In the device APK or IPA In file . To ensure that your app makes all changes on the device , Consider stopping and running the app again .

state What does that mean? ? What is? setState()?

** Simply speaking ,“ state ” Is a collection of widget variable values .** Anything that can be changed , Such as counter counting 、 Text etc. , Can be State Part of . Imagine a counter application , The main dynamic is counter counting . When the count changes , The screen needs to be refreshed to display the new value .setState() It is essentially a way to tell an application to refresh and rebuild the screen with new values .

What are stateful and stateless widgets ?

TL;DR: The widgets that allow you to refresh the screen are stateful widgets . Stateless widgets are stateless .

In more detail , A dynamic widget whose content can be changed should be a stateful widget . Stateless widgets can only change content when changing parameters , So it needs to be done above the location point in the widget hierarchy . A screen or widget that contains static content should be a stateless widget , But change the content , There needs to be a state .

How do you deal with Flutter Indentation and structure in code ?

Android Studio Provides tools to simplify Flutter The structure of the code . The two main points are :

  1. Alt + Enter/ Command + Enter: This makes it easy to wrap and delete widgets and swap widgets in complex hierarchies . In order to use it , Simply point the cursor at the widget declaration and press the key to provide some options . It sometimes feels like a godsend .
  2. DartFMT:dartfmt Format your code to keep the hierarchy and indentation clean . After you accidentally move a few parentheses , It will make your code more beautiful .

Why do we pass functions to widgets ?

We pass a function to a widget , In essence, it means ,“ Call this function when something happens ”. The function is Dart The first type of object in , It can be passed as an argument to other functions . Use Android (<Java 8) There are too many boilerplate codes for simple callbacks .

Java Callback :

button.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View view) {
      // Do something here
    }
  }
);

( Be careful , This is just the code to set up the listener . Defining a button requires a separate XML Code .)

Dart equivalent:

FlatButton(
  onPressed: () {
    // Do something here
  }
)

(Dart Both declaration and callback settings .)

It becomes clearer and more organized , And help us avoid unnecessary complications

原网站

版权声明
本文为[nuts]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210821105037103D.html