Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js/object dart/map sample added #32

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -74,6 +74,7 @@ How to contribute new examples:
- [Andrei Lesnitsky](https://twitter.com/lesnitsky_a)
- [Vyacheslav Egorov](https://twitter.com/mraleph)
- [Tiago Almeida](https://twitter.com/TiagoHenriq)
- [Taha Aytekin](https://github.com/tahaemin)

## License

@@ -21,6 +21,7 @@ import LoadingIndicatorWhileSmthIsLoading from './loading-indicator-while-smth-i
import Navigation from './navigation.md';
import Debounce from './debounce.md';
import CheckIfDev from './check-if-dev.md';
import ObjectMap from './object-map.md';

<div className="toc-container">

@@ -126,3 +127,7 @@ import CheckIfDev from './check-if-dev.md';
## Debounce

<Debounce />

## Object / Map

<ObjectMap />
@@ -0,0 +1,73 @@
import Example from '../components/md/Example';

<Example reactnative>

Defining Object
```js
const exampleObject = {
type: 'example',
value: 3,
children: null,
isEditable: false
}
```

Accessing Object values
```js
exampleObject.type;
exampleObject.value;
```

</Example>

<Example flutter>

In Dart, there is Map like JS Objects but it is a little bit different

This comment has been minimized.

Copy link
@lesnitsky

lesnitsky May 13, 2019

Owner

Map like JS Objects is a bit confusing statement. JS objects are actual maps (if we're not speaking about shadow classes which are created under the hood), so I'd drop this comment completely


Defining Map
```dart
Map<String, dynamic> exampleMap = {

This comment has been minimized.

Copy link
@lesnitsky

lesnitsky May 13, 2019

Owner

You can just use final, dart is smart enough to figure out the type of this literal

'type': 'example',
'value': 3,
'children': null,
'isEditable': false,
}
```

Accessing Map values
```dart
exampleMap['type'];
exampleMap['value'];
```

If you want to define and access Map like JS, you need to define a class

This comment has been minimized.

Copy link
@lesnitsky

lesnitsky May 13, 2019

Owner

that's not technically correct, class is a user defined type, but Map is a data structure, so I'd get rid of this piece of code

```dart
class ExampleClass {
final String type;
final int value;
final List children;
final bool isEditable;
ExampleClass({
this.type,
this.value,
this.children,
this.isEditable,
});
}
ExampleClass exampleClass = ExampleClass(
type: 'example',
value: 3,
children: null,
isEditable: false,
);
```

Accessing Class values
```dart
exampleClass.type;
exampleClass.value;
```

</Example>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.