Dart is an open source, purely object-oriented, optionally typed, and a class-based language which has excellent support for functional as well as reactive programming. Dart was the fastest-growing language between 2018 and 2019, with usage up a massive 532%. Follow along and check 22 common Dart Interview Questions Flutter and Mobile developers should be prepared for in 2020.
Dart is an object-oriented, garbage-collected programming language that you use to develop Flutter apps. It was also created by Google, but is open-source, and has community inside and outside Google. Dart was chosen as the language of Flutter for the following reason:
The fat arrow syntax is simply a short hand for returning an expression and is similar to (){ return expression; }.
The fat arrow is for returning a single line, braces are for returning a code block.
Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;). For example, you can’t put an if statement there, but you can use a conditional expression
// Normal function
void function1(int a) {
if (a == 3) {
print('arg was 3');
} else {
print('arg was not 3');
}
}
// Arrow Function
void function2(int a) => print('arg was ${a == 3 ? '' : 'not '}3');Both named and positional parameters are part of optional parameter:
Optional Positional Parameters:
[ ] is a positional optional parameter. ```dart
getHttpUrl(String server, String path, [int port=80]) {
// ...
}
``` ```dart
getHttpUrl(String server, String path, [int port=80, int numRetries=3]) {
// ...
}
```
In the above code, `port` and `numRetries` are optional and have default values of 80 and 3 respectively. You can call `getHttpUrl` with or without the third parameter. The optional parameters are _positional_ in that you can't omit `port` if you want to specify `numRetries`.Optional Named Parameters:
{ } is a named optional parameter. ```dart
getHttpUrl(String server, String path, {int port = 80}) {
// ...
}
``` ```dart
getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) {
// ...
}
```
You can call `getHttpUrl` with or without the third parameter. You **must** use the parameter name when calling the function. ```dart
getHttpUrl('example.com', '/index.html', numRetries: 5, port: 8080);
getHttpUrl('example.com', '/index.html', numRetries: 5);
```Required Parameters
Dart required parameters are the arguments that are passed to a function and the function or method required all those parameters to complete its code block.
findVolume(int length, int breath, int height) {
print('length = $length, breath = $breath, height = $height');
}
findVolume(10,20,30);Optional Parameters
{ }
- eg. getUrl(int color, [int favNum])
- Positional
- Parameters wrapped by [ ])
- eg. getUrl(int color, {int favNum})
- Default
- Assigning a default value to a parameter.
- eg. getUrl(int color, [int favNum = 6])There are two kinds of streams. 1. Single subscription streams - The most common kind of stream. - It contains a sequence of events that are parts of a larger whole. Events need to be delivered in the correct order and without missing any of them. - This is the kind of stream you get when you read a file or receive a web request. - Such a stream can only be listened to once. Listening again later could mean missing out on initial events, and then the rest of the stream makes no sense. - When you start listening, the data will be fetched and provided in chunks.
async void method is completed in Dart?Changing the return type to Future<void>.
Future<void> save(Folder folder) async {
.....
}Then you can do await save(...); or save().then(...);
whenCompleted() different from then() in Future?.whenComplete will fire a function either when the Future completes with an error or not, while .then returns a new Future which is completed with the result of the call to onValue (if this future completes with a value) or to onError (if this future completes with an error).whenCompleted is the asynchronous equivalent of a "finally" block.Consider the code:
final List<Ball> _ballList =[Ball(),Ball(),Ball(),Ball(),Ball(),]What can to be done in order to not repeat Ball() multiple times.
Using collection-for if we need different instances of Ball()
final List<Ball> _ballList = [
for (var i = 0; i < 5; i += 1) Ball(),
];If we need the same instance of Ball(), List.filled should be used
final List<Ball> _ballList = List<Ball>.filled(5, Ball());Async functions are normal functions with some sugar on top. The function variable type just needs to specify that it returns a Future:
class Example {
Future<void> Function() asyncFuncVar;
Future<void> asyncFunc() async => print('Do async stuff...');
Example() {
asyncFuncVar = asyncFunc;
asyncFuncVar().then((_) => print('Hello'));
}
}
void main() => Example();Consider you have two lists [1,2,3,4,5,6,7] and [3,5,6,7,9,10]. How would you get the difference as output? eg. [1, 2, 4]
You can do something like this:
List<double> first = [1,2,3,4,5,6,7];
List<double> second = [3,5,6,7,9,10];
List<double> output = first.where((element) => !second.contains(element));alternative answer:
List<double> first = [1,2,3,4,5,6,7];
List<double> second = [3,5,6,7,9,10];
List<double> output = [];
first.forEach((element) {
if(!second.contains(element)){
output.add(element);
}
});
//at this point, output list should have the answernote that for both cases, you need to loop over the larger list.
Future and Stream classes. Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
await for (var value in stream) {
sum += value;
}
return sum;
}listen() from the Stream API.Null-aware operators? int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3. print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.async, await in Flutter/Dart?List into a Map in Dart?AOT work?._() mean in Dart/Flutter??? and ?.async and async* in Dart?Rust has been Stack Overflow’s most loved language for four years in a row and emerged as a compelling language choice for both backend and system developers, offering a unique combination of memory safety, performance, concurrency without Data races...
Clean Architecture provides a clear and modular structure for building software systems, separating business rules from implementation details. It promotes maintainability by allowing for easier updates and changes to specific components without affe...
Azure Service Bus is a crucial component for Azure cloud developers as it provides reliable and scalable messaging capabilities. It enables decoupled communication between different components of a distributed system, promoting flexibility and resili...