oppo2020秋招面试
The first alpha version of Flutter appeared almost three years ago — in May 2017. No one back then thought it’d become so popular in such a short amount of time. It’s already gotten more stars on GitHub than its biggest competitor, React Native, and since the stable release in December 2018, more companies started to noticing its potential and are looking for developers willing to write applications in Flutter.
Flutter的第一个alpha版本大约在三年前-2017年5月出现。当时没有人认为它在这么短的时间内变得如此流行。 自从其最大竞争对手React Native以来,它在GitHub上已经获得了更多的赞誉,并且自2018年12月稳定版发布以来,越来越多的公司开始意识到其潜力,并正在寻找愿意用Flutter编写应用程序的开发人员。
Maybe you’re now bored with your current technology or just want to simply learn Flutter for your side projects. You might be wondering what you should know about Dart and Flutter. Or maybe you’re having an interview for a new job as Flutter developer. Whatever the case, I hope my article can help you prepare better for it.
也许您现在对当前的技术感到无聊,或者只是想为自己的辅助项目学习Flutter。 您可能想知道有关Dart和Flutter的知识。 也许您正在接受Flutter开发人员的新工作面试。 无论如何,我希望我的文章可以帮助您更好地为此做准备。
Even though some (or most) of the questions might seem trivial to you, let’s be honest … the stable version hasn’t been release for that long, so I doubt anyone expects a lot of experience yet. From what I see, knowing some of the basics of Flutter and Dart is enough as long as you have good experience with either Android or iOS.
即使某些(或大多数)问题对您来说似乎微不足道,但老实说……稳定版本尚未发布太久了,所以我怀疑有人期望获得很多经验。 据我所知,只要您对Android或iOS有良好的经验,了解Flutter和Dart的一些基础知识就足够了。
Hopefully, we won’t see jobs offers like:
希望我们不会看到像这样的工作机会:
“Requirements: 5 years experience with Flutter.”
“要求:有5年Flutter工作经验。”
Wait, what?
等一下
Also, if you have any feedback regarding questions or if you know of any good ones — feel free to let me know, and I’ll try to keep this list updated.
另外,如果您对问题有任何反馈或知道有任何好的问题,请随时告诉我,我将尽力保持此列表的最新状态。
镖 (Dart)
谁创建了Dart? (Who Created Dart?)
Dart was created by Google in 2011. It’s an open-source, scalable programming language, with robust libraries and runtimes, for building web, server, and mobile apps.
Dart由Google于2011年创建。Dart是一种开源,可扩展的编程语言,具有健壮的库和运行时,用于构建Web,服务器和移动应用程序。
什么是 'this'
关键字,什么是它的构造函数创建时的目的? (What is the 'this'
keyword, and what is its purpose when creating constructors?)
The this
keyword may be used in constructors as a handy shortcut for assigning values to properties — e.g., MyClass(this.param, this.param2, this.param3);
.
this
关键字可以在构造函数中用作为属性分配值的便捷捷径-例如, MyClass(this.param, this.param2, this.param3);
。
可选位置参数和可选名称参数之间有什么区别? (What’s the difference between optional-positional and optional-named parameters?)
When using named parameters, we don’t have to keep the same order as in the declaration and can just pass parameters by using its names — e.g., doSomething(‘First’, prefix: ‘1.’)
.
使用命名参数时,我们不必与声明中的顺序保持一致,而可以使用其名称来传递参数,例如, doSomething('First', prefix: '1.')
。
什么是扩展,我们如何创建扩展? (What are extensions, and how do we create one?)
Extension methods make it possible to add new functionalities to any type, even those from the foundation. They were introduced in Dart 2.7 and are already available to use in Flutter as well.
扩展方法可以为任何类型添加新功能,甚至是基础功能。 它们在Dart 2.7中引入,并且已经可以在Flutter中使用。
extension NumberParsing on String {
int parseInt() {
return int.parse(this);
}
}
我们可以从动态类型的扩展中调用方法吗? (Can we call a method from an extension on dynamic types?)
No, it’s not possible. The reason for that is extension methods are resolved against the static type of the receiver. Because extension methods are resolved statically, they’re as fast as calling a static function.
不,不可能。 其原因是扩展方法是针对接收者的静态类型解决的。 由于扩展方法是静态解析的,因此它们与调用静态函数一样快。
我们如何定义通用扩展? (How can we define a generic extension?)
We can do this by adding a template parameter, such as:
我们可以通过添加模板参数来做到这一点,例如:
extension MyFancyList on List {
int get doubleLength => length * 2;
List operator -() => reversed.toList();
List> split(int at) => >[sublist(0, at), sublist(at)];
}
您如何创建工厂? (How do you create a factory?)
Factory constructors are created by adding a factory
keyword. Those can return subtypes or even null
— e.g., factory User.fromXml(String xml)
.
工厂构造函数是通过添加factory
关键字创建的。 这些可以返回子类型,甚至可以返回null
—例如, factory User.fromXml(String xml)
。
什么是空感知运算符,我们如何使用它? (What is a null-aware operator, and how do we use it?)
There are two null-aware operators — the first one is ??
, and the second is ??=
.
有两个null感知运算符-第一个是??
,第二个是??=
。
Example: int x; x ??= 1
, var x = y ?? 2
示例: int x; x ??= 1
int x; x ??= 1
, var x = y ?? 2
var x = y ?? 2
我们如何有条件地访问Dart中的属性或方法? (How can we conditionally access a property or method in Dart?)
We can conditionally access a property or method on the object by using the ?.
operator — e.g., x?.doSomething()
.
我们可以使用?.
有条件地访问对象的属性或方法?.
运算符,例如x?.doSomething()
。
我们如何创建自定义的getter和setter,这样做的好处是什么? (How do we create custom getters and setters, and what’s the advantage of doing so?)
By doing so, we can add some additional checks while setting a value to make sure it fulfills our criteria. Also, we can control the access-level getter and setter separately.
这样,我们可以在设置值以确保它满足我们的标准时添加一些其他检查。 另外,我们可以分别控制访问级别的获取器和设置器。
class MyClass {
int _aProperty = 0;
int get aProperty => _aProperty;
set aProperty(int value) {
if (value >= 0) {
_aProperty = value;
}
}
}
Dart中的“未来”是什么? (What’s a ‘Future’ in Dart?)
It’s used for asynchronous programming. A Future
represents a potential value (or error) that’ll be available at some time in the future.
它用于异步编程。 Future
表示将来某个时候可用的潜在价值(或错误)。
Receivers of a Future
can register callbacks that handle the value or error once it’s available.
Future
接收者可以注册一旦值或错误可用的回调。
什么是 the 'async'
和 'await'
关键字? (What are the 'async’
and 'await’
keywords for?)
These are keywords that make asynchronous programming much easier to use and read.
这些关键字使异步编程更易于使用和阅读。
We can use those instead of writing a then
callback on Future
objects.
我们可以使用它们而不是在Future
对象上编写then
回调。
使用Dart时有哪些良好的风格习惯? (What are good style practices while working with Dart?)
You need to be aware of how to name files, classes, properties, etc. correctly. You should read this and remember as much as you can.
您需要知道如何正确命名文件,类,属性等。 你应该阅读这并记住尽可能多的,你可以。
我们如何解析JSON? (How can we parse JSON?)
Dart provides this natively, and it’s available within the dart:convert library. There are jsonDecode
and jsonEncode
methods available.
Dart本身提供了此功能,并且在dart:convert库中可用。 有可用的jsonDecode
和jsonEncode
方法。
什么是点差算子? (What’s a spread operator?)
The spread operator was added to Dart 2.3. It provides a concise way to insert multiple elements into a collection. There’s also a null-aware
spread operator that inserts elements conditionally (only if the array isn’t nil
).
点差运算符已添加到Dart 2.3中。 它提供了一种将多个元素插入到集合中的简洁方法。 还有一个null-aware
传播运算符,可有条件地插入元素(仅当数组不是nil
)。
关于Dart的最后想法 (Last thoughts on Dart)
That’s just a few questions you might be asked about at the interview. You may expect more general questions. If it’s a junior position, it could even be anything about flow control statements (if-else, loops, switch, asserts, etc.) or even the most basic questions like “What’s a class?”
这只是您在面试中可能会被问到的几个问题。 您可能会期望更多一般性问题。 如果是初级职位,那么它甚至可能与流控制语句(if-else,循环,切换,断言等)有关,甚至与诸如“什么是类?”之类的最基本问题有关。
扑 (Flutter)
Flutter的稳定版本何时发布? (When was the stable version of Flutter released?)
December 2019.
2019年12月。
什么是小部件? (What are widgets?)
Everything in Flutter is a widget, but what exactly is a widget? A widget is a user interface element.
Flutter中的所有内容都是小部件,但是什么是小部件? 小部件是用户界面元素。
无状态小部件和有状态小部件之间有什么区别? (What’s a difference between stateless and stateful widgets?)
Stateless widgets have their state created only once — therefore, the build
method is called only once as well.
无状态小部件的状态仅创建一次-因此, build
方法也仅被调用一次。
Stateful widgets have their state explicitly created, and it can be modified during the lifetime of the widget.
有状态的小部件具有明确创建的状态,可以在小部件的生命周期内对其进行修改。
什么是“ StatefulWidget”的生命周期? (What’s the lifecycle of a ‘StatefulWidget’?)
createState()
— a method in aStatefulWidget
that’s called immediately and should return aState
object for the widgetcreateState()
—StatefulWidget
中的一种方法,将立即调用该方法,该方法应返回小部件的State
对象initState()
— the first method called in theState
object after the widget is createdinitState()
—创建窗口小部件后在State
对象中调用的第一个方法didChangeDependencies()
— called immediately afterinitState()
the first time the widget is builtdidChangeDependencies()
-首次构建窗口小部件后在initState()
之后立即调用build()
— very similar to the one from theStatelessWidget
. It’s called right afterdidChangeDependencies()
. It’s called every single time the UI needs to render and returns a widget’s tree.build()
—与StatelessWidget
非常相似。 在didChangeDependencies()
之后立即调用它。 UI每次需要渲染并返回小部件的树时都会调用它。didUpdateWidget()
— it’s called when the parent widget changes and needs to redraw its UIdidUpdateWidget()
—当父小部件更改并且需要重绘其UI时调用deactivate()
— called beforedispose()
, when this object is removed from the treedeactivate()
—在从树中删除此对象时在dispose()
之前调用dispose()
— called when deallocating the whole widget and its statedispose()
—在取消分配整个小部件及其状态时调用
什么是pubspec.yml文件? (What’s a pubspec.yml file?)
It’s a file where you can provide all the dependencies (optional and required — e.g., plugins, Dart version, font, images) of your Flutter project. It’s also a place where you configure the project’s name and description.
它是一个文件,您可以在其中提供Flutter项目的所有依赖项(可选和必需-例如,插件,Dart版本,字体,图像)。 它也是您配置项目名称和描述的地方。
什么是热重装和热重启? 有什么不同? (What’s a hot reload and a hot restart? What’s the difference?)
The hot-reload feature allows you to quickly reload the code on a running app. It takes less time than hot restart, as the hot restart destroys and recreates all states in the app as well (and the whole widget tree is completely rebuilt).
热重载功能使您可以在正在运行的应用程序中快速重载代码。 与热重启相比,它花费的时间更少,因为热重启还可以销毁并重新创建应用程序中的所有状态(并且整个小部件树都已完全重建)。
什么是“ InheritedWidget”? (What’s an ‘InheritedWidget’?)
It’s a special kind of widget that only holds data — it doesn’t describe the user interface. It defines a context at the root of a subtree, and it can efficiently provide this context to every widget down the tree.
这是一种特殊的小部件,仅包含数据,没有描述用户界面。 它在子树的根部定义了一个上下文,并且可以有效地将该上下文提供给树下的每个小部件。
Watch this video to understand the whole concept — I think it’s well explained there.
观看此视频,以了解整个概念-我认为在此有充分的解释。
Flutter如何与Objective-C / Swift / Kotlin / Java中的本机框架通信? (How can Flutter communicate with native frameworks in Objective-C/Swift/Kotlin/Java?)
Through plugins. To call any native code, we need to create a bridge between the native code and Flutter.
通过插件。 要调用任何本机代码,我们需要在本机代码和Flutter之间建立桥梁。
什么是“ SafeArea”小部件? (What’s a ‘SafeArea’ widget for?)
It’s a widget that makes it easily to place our widgets in a portion of the view that’s unobscured by bars and other content like status bars.
它是一个小部件,可以很容易地将我们的小部件放置在视图的一部分中,该部分不会被条和状态栏之类的其他内容所遮盖。
Flutter仅适用于移动应用程序吗? (Is Flutter only for mobile apps?)
No, it’s possible to build web applications as well — although it’s still in beta and not officially released yet.
不,虽然它仍处于测试阶段且尚未正式发布,但也可以构建Web应用程序。
创建导航有哪些不同的方法? (What are the different ways to create navigation?)
There are three ways to create navigation in Flutter:
有三种方法可以在Flutter中创建导航:
Direct navigation with
MaterialPageRoute
(or similar)使用
MaterialPageRoute
(或类似方法)直接导航Static navigation with a map route — where you create a
routes
map in theMaterialApp
and then push it by using its name带有地图路线的静态导航-您可以在
MaterialApp
创建routes
地图,然后使用其名称进行推送Dynamic navigation which generates routes — i.e., implementing
onGenerateRoute
callback in theMaterialApp
class动态导航生成路线,即在
MaterialApp
类中实现onGenerateRoute
回调
什么是条子? 举一些例子。 (What are slivers? Give some examples.)
You’re on a good start if you already noticed they’re not called silvers. A sliver is a portion of a scrollable area. You can use slivers to achieve custom scrolling effects. That means you can, for example, have a ListView
and GridView
scroll together. It’d be possible to do something similar with a basic scroll as well, but slivers are performing much better in such cases.
如果您已经注意到它们不被称为银牌,那么您就处于一个良好的开端。 条子是可滚动区域的一部分。 您可以使用条子来实现自定义滚动效果。 这意味着,例如,您可以使ListView
和GridView
一起滚动。 也可以用基本的卷轴做类似的事情,但是在这种情况下条子的性能要好得多。
我们如何在条子中嵌入常规小部件(例如,容器)? (How can we embed a regular widget (e.g., a container) within a sliver?)
We can embed a container within SliverToBoxAdapter
that can be used as a child of the slivers.
我们可以在SliverToBoxAdapter
中嵌入一个容器,该容器可以用作条子。
什么是补间动画? (What are tween animations?)
Tween animations is short for in-betweening. In tween animations, we define the beginning and the end of the animation, and the framework itself calculates how to transition the widget to fulfill values.
Tween动画是中间 动画的缩写。 在补间动画中,我们定义动画的开始和结束,框架本身计算如何转换小部件以实现值。
您熟悉哪些架构,并且更喜欢哪种架构? (What architectures are you familiar with, and which one do you prefer?)
So it’s quite up to you. You should know at least one if you’ve ever created an app in Flutter. If not, you might want to read about them a little bit — e.g., BLoC, Redux.
因此完全取决于您。 如果您曾经在Flutter中创建过一个应用,则应该至少知道一个。 如果没有,您可能需要阅读一些有关它们的信息,例如BLoC,Redux。
什么是钥匙,什么时候使用它们? (What are keys, and when do you use them?)
A key is an identifier that can be used for widgets, elements, and semantic nodes. Keys can be useful if you want to, for example, preserve a state when widgets move around in your widget tree. That means, if you use the key on a ScrollView
, you can preserve the scroll position when modifying a collection.
密钥是可用于小部件,元素和语义节点的标识符。 例如,如果您想保留小部件在小部件树中四处移动的状态,则键很有用。 这意味着,如果您在ScrollView
上使用键,则可以在修改集合时保留滚动位置。
您如何仅在某些模式(发布,调试,配置文件)下执行代码? (How do you execute code only in certain modes (release, debug, profile)?)
You can use constants from the Foundation package to check what mode the app is running in: kReleaseMode
, kProfileMode
, or kDebugMode
.
您可以使用Foundation包中的常量来检查应用程序以哪种模式运行: kReleaseMode
, kProfileMode
或kDebugMode
。
调试和配置文件模式之间有什么区别? (What’s a difference between the debug and profile modes?)
The debug mode is used for debugging the application. However, in the profile mode, only some debugging ability is maintained — just enough to profile your app’s performance.
调试模式用于调试应用程序。 但是,在配置文件模式下 ,仅保留了一些调试功能-仅足以描述应用程序的性能。
什么时候使用 'double.infinity'
? (When do you use 'double.infinity’
?)
It’s used when you want to specify the widget should be as big as a parent.
当您要指定小部件应与父部件一样大时使用。
如何创建在iOS和Android中看起来不同的小部件? (How do you create a widget that looks different in iOS and Android?)
You can use the flutter_platform_widgets plugin, which makes things easier to handle, or you can check the platform manually by doing:
您可以使用flutter_platform_widgets插件,这使事情更易于处理,也可以通过执行以下操作手动检查平台:
Platform.isIOS
from dart:io
package
dart:io
包中的Platform .isIOS
or
要么
if(Theme.of(context).platform == TargetPlatform.iOS)
if(Theme.of(context).platform == TargetPlatform.iOS)
Then, create/configure widgets depending on it.
然后,根据其创建/配置窗口小部件。
如何在Flutter中发出HTTP请求? (How do you make HTTP requests in Flutter?)
The easiest way is to use the HTTP package (import ‘package:http/http.dart’ as http;
).
最简单的方法是使用HTTP包( import 'package:http/http.dart' as http;
)。
This has a convenient method for making a request:
这具有发出请求的便捷方法:
http.get(‘https://jsonplaceholder.typicode.com/albums/1');
This returns a Future
.
这将返回Future
。
您如何使用十六进制颜色? (How do you use hexadecimal colors?)
const Color(0xFF000000);
const Color(0xFF000000);
如何创建具有初始值的TextField? (How to create a TextField with initial value?)
We need to set a controller with an initial value — TextEditingController(text: “Initial Text”)
.
我们需要设置一个具有初始值的控制器— TextEditingController(text: “Initial Text”)
。
有什么办法以编程方式关闭键盘吗? (Is there any way to dismiss the keyboard programatically?)
FocusScope.of(context).unfocus();
FocusScope.of(context).unfocus();
有什么方法可以向列添加“ ListView”? (What are the ways to add a ‘ListView’ to a column?)
We need to either explicitly set the height of the ListView
, use the shrinkWrap
property on the ListView
, or wrap the ListView
with an Expanded
widget.
我们需要明确地设定的高度ListView
,使用shrinkWrap
对物业ListView
,或包裹ListView
与Expanded
部件。
Which one you use depends on your design.
您使用哪一种取决于您的设计。
黄色双下划线的 文字后面是什么原因 ? (What’s the reason behind text with a yellow double underline?)
It means an instance of the theme is missing as a parent.
这意味着主题的一个实例作为父级缺少。
我们可以将SVG文件用作图像吗? (Can we use SVG files as images?)
Yes, the flutter_svg plugin allows it.
是的, flutter_svg插件允许它。
如何从网络加载图像? (How do you load an image from the network?)
To show images from an URL, we can use the Image.network()
constructor.
要显示来自URL的图像,我们可以使用Image.network()
构造函数。
您如何以编程方式滚动 'ScrollView'
小部件? (How do you programatically scroll a 'ScrollView'
widget?)
You need to create a controller, ScrollController
, and assign it to the ScrollView
. Then, we can call the animateTo
method (or another similar method) on the controller whenever we want to scroll.
您需要创建一个控制器ScrollController
,并将其分配给ScrollView
。 然后,只要我们想滚动,就可以在控制器上调用animateTo
方法(或其他类似方法)。
您如何覆盖后退按钮动作? (How do you override the back-button action?)
We can use the WillPopScope
widget for this.
我们可以为此使用WillPopScope
小部件。
'ListView'
小部件 上 the 'reverse''
属性 的用途是什么 ? (What’s the purpose of the 'reverse‘’
property on a 'ListView’
widget?)
It shows the items on the list in reversed order. We could reverse the array with the items as well, but this one makes the code cleaner.
它以相反的顺序显示列表中的项目。 我们也可以将数组与项目互换,但这可以使代码更简洁。
如何根据屏幕尺寸设置小部件的尺寸? (How do you set a widget’s size based on the screen size?)
MediaQuery.of(context).size
MediaQuery.of(context).size
更新所有插件的命令是什么? (What’s the command for updating all of the plugins?)
It’s flutter pub upgrade
.
这是flutter pub upgrade
。
颤抖的树在摇什么? (What’s Flutter tree shaking?)
It means that all the code we don’t actually use (dead code) won’t be compiled. Thanks to that, we can use big libraries without including all the unused code in our apps.
这意味着我们实际上不使用的所有代码(死代码)都不会被编译。 因此,我们可以使用大型库,而无需在应用程序中包含所有未使用的代码。
Flutter实际上是本地人吗? (Is Flutter actually native?)
The Flutter application is compiled into a native ARM and x86 libraries, so, technically, it’s native.
Flutter应用程序被编译成本地ARM和x86库,因此从技术上讲它是本地的。
其他 (Other)
I think it’s also important to have a basic understanding of each platform (iOS and Android) and to know some platform-specific questions. For example:
我认为对每个平台(iOS和Android)有基本了解并了解一些平台特定的问题也很重要。 例如:
What’s the
AndroidManifest.xml
file?什么是
AndroidManifest.xml
文件?What’s the
Info.plist
file?什么是
Info.plist
文件?- What are provisioning profiles? 什么是配置配置文件?
How do you change the bundle identifier/
ApplicationId
?如何更改包标识符/
ApplicationId
?- How do you set an application icon in Android and iOS? 如何在Android和iOS中设置应用程序图标?
结论 (Conclusion)
Even though Flutter has been on the market since May 2017 and it’s still quite new, employers are already recruiting developers with this skill.
尽管Flutter自2017年5月以来一直在市场上上市,而且仍然很新,但雇主已经在招募具有这种技能的开发人员。
If you already have a background and a knowledge of any of the mobile platforms (iOS, Android, or, even better, if you know both at least at a basic level), you might be a great fit and will probably perform very well from the very beginning of you career as Flutter developer.
如果您已经有背景并且对任何移动平台(iOS,Android,或者更好的是,如果您至少在基本的方面都知道的话)有所了解,那么您可能非常适合,并且可能会在作为Flutter开发人员的职业生涯的开始。
If it’s your first job, then you might have a lot of things to catch up on — as you’ll have to know how to do certain things on both Android and iOS systems. There are also some differences between them that are good to know.
如果这是您的第一份工作,那么您可能有很多事情要赶上-因为您必须知道如何在Android和iOS系统上做某些事情。 他们之间也有一些不同之处,很容易知道。
If you’re an iOS Developer and have tried SwiftUI and loved it — you’ll feel at home since both frameworks use the concept of declarative programming.
如果您是iOS开发人员并且尝试过SwiftUI并喜欢它-您会感到宾至如归,因为两个框架都使用了声明式编程的概念。
Dart has been here for a lot longer. It’s an object-oriented programming language, so if you know another one already, you shouldn’t have many problems getting used to it. A lot of frameworks (e.g., Rx) are available as well, so depending on what architecture you decide to go with, you might have a really good time writing in Dart.
Dart已经在这里待了很长时间了。 它是一种面向对象的编程语言,因此,如果您已经知道另一种语言,那么习惯它就不会有很多问题。 也有很多框架(例如Rx)可用,因此根据您决定采用哪种架构,您可能会在Dart中花费很多时间。
I hope you liked the article, and hopefully it’ll help you learn Dart and Flutter. Maybe you’ll eventually get your dream job, if that’s what you’re looking for!
希望您喜欢这篇文章,并希望它能帮助您学习Dart和Flutter。 如果您正在寻找的话,也许您最终将获得理想的工作!
Thank you for reading, and happy coding!
感谢您的阅读,并祝您编程愉快!
翻译自: https://medium.com/better-programming/flutter-interview-questions-and-answers-2020-adad5dacaf6a
oppo2020秋招面试