Dachang interview iOS real questions sorting (flutter)

Flutter's interview is actually similar to the Android interview routine, and it is divided into two parts:

1.Dart

2.Flutter

After all, if Flutter uses Dart to write, it must understand some relevant knowledge points of Dart.


Dart related interview questions

1. What does ".." in Dart mean?

The ".." in Dart means "cascading operator" and is used for configuration convenience.

The difference between ".." and "." is that after calling "..", the return is equivalent to this, while "." returns the value returned by the method.

2. Dart Scope

Dart does not have keywords such as "public" and "private". It is public by default. Private variables start with an underscore  _.

3. Is Dart a single-threaded model? How does it work?

Dart is a single-threaded model. See this picture for how it works:

Quoting from "Flutter Chinese Network":

Dart runs with a message loop mechanism in a single thread, which contains two task queues, one is the "microtask queue"  microtask queue , and the other is called the "event queue"  event queue .

After the entry function main() is executed, the message loop mechanism is started. First, the tasks in the micro-task queue will be executed one by one in the order of first-in, first-out. When all the micro-task queues are executed, the tasks in the event queue will be executed. After the event tasks are executed, the micro-tasks will be executed. nonstop.


More interview information

4. How does Dart multitasking work in parallel?

As I said just now, since Dart does not have multi-threading, how to perform multi-task parallelism?

Dart provides an  independent running worker - isolate that is similar to a new thread, but does not share memory .

So how do they interact?

Here are some answers from Concurrent Programming, Asynchronous and Event-Driven Detailed Explanation in Dart for Getting Started with Flutter:

在dart中,一个Isolate对象其实就是一个isolate执行环境的引用,一般来说我们都是通过当前的isolate去控制其他的isolate完成彼此之间的交互,而当我们想要创建一个新的Isolate可以使用Isolate.spawn方法获取返回的一个新的isolate对象,两个isolate之间使用SendPort相互发送消息,而isolate中也存在了一个与之对应的ReceivePort接受消息用来处理,但是我们需要注意的是,ReceivePort和SendPort在每个isolate都有一对,只有同一个isolate中的ReceivePort才能接受到当前类的SendPort发送的消息并且处理。

5. 说一下 Future?

Future,字面意思「未来」,是用来处理异步的工具。

刚才也说过:

Dart 在单线程中是以消息循环机制来运行的,其中包含两个任务队列,一个是“微任务队列” microtask queue,另一个叫做“事件队列” event queue

Future 默认情况下其实就是往「事件队列」里插入一个事件,当有空余时间的时候就去执行,当执行完毕后会回调 Future.then(v) 方法。

而我们也可以通过使用 Future.microtask 方法来向 「微任务队列」中插入一个任务,这样就会提高他执行的效率。

因为在 Dart 每一个 isolate 当中,执行优先级为 :Main > MicroTask > EventQueue

6. 说一下 Stream?

Stream 和 Feature 一样,都是用来处理异步的工具。

但是 Stream 和 Feature 不同的地方是 Stream 可以接收多个异步结果,而Feature 只有一个。

Stream 的创建可以使用 Stream.fromFuture,也可以使用 StreamController 来创建和控制。

还有一个注意点是:普通的 Stream 只可以有一个订阅者,如果想要多订阅的话,要使用 asBroadcastStream()

7. 说一下 mixin?

关于什么是 mixin,引用 张风捷特烈 文章中的:

首先mixin是一个定义类的关键字。直译出来是混入,混合的意思 Dart为了支持多重继承,引入了mixin关键字,它最大的特殊处在于: mixin定义的类不能有构造方法,这样可以避免继承多个类而产生的父类构造方法冲突

Flutter 相关面试题

1. StatefulWidget 的生命周期

initState():Widget 初始化当前 State,在当前方法中是不能获取到 Context 的,如想获取,可以试试 Future.delayed()didChangeDependencies():在 initState() 后调用,State对象依赖关系发生变化的时候也会调用。•deactivate():当 State 被暂时从视图树中移除时会调用这个方法,页面切换时也会调用该方法,和Android里的 onPause 差不多。•dispose():Widget 销毁时调用。•didUpdateWidget:Widget 状态发生变化的时候调用。

借用 CoorChice 文章 里的一张图:

2. Flutter 如何与 Android iOS 通信?

Flutter 通过 PlatformChannel 与原生进行交互,其中 PlatformChannel 分为三种:

1.BasicMessageChannel:用于传递字符串和半结构化的信息。2.MethodChannel:用于传递方法调用。Flutter主动调用Native的方法,并获取相应的返回值。3.EventChannel:用于数据流(event streams)的通信。

具体可以查看 闲鱼技术:深入理解 Flutter Platform Channel[4]

3. 什么是 Widgets、RenderObjects 和 Elements?

Widget 仅用于存储渲染所需要的信息。•RenderObject 负责管理布局、绘制等操作。•Element 才是这颗巨大的控件树上的实体。

具体可以查看 [译] Flutter,什么是 Widgets、RenderObjects 和 Elements?[5]

4. 说一下什么是状态管理,为什么需要它?

首先状态其实是一个概念上的东西,区分全局状态和局部状态。

局部状态比如说一个控件中输入的信息,全局状态比如是登陆后从后台请求回来的 userId。

当全局状态越来越多,多个页面共享一个状态时,我们就需要管理它。

常用的状态管理有:

•ScopedModel•BLoC•Redux / FishRedux•Provider

5. 说一下 BLoC 模式?

这里引用一部分:

BLoC是一种利用reactive programming方式构建应用的方法,这是一个由流构成的完全异步的世界。

6. 如何统一管理错误页面?

我们都知道,如果在 Flutter 当中出错的话,那就是一片红。

可以使用 ErrorWidget.builder 来自定义一个 Widget 就 ok 了。

总结

暂时就写到这,写了这么多,自己对 Flutter & Dart 的基础认识也更深了,也欢迎各路大佬交流。

Guess you like

Origin juejin.im/post/6993635273389113352