As an engineer who has developed React/React Native, you will encounter this situation when you first start to learn about Flutter development. For a certain function, you are very skilled in using certain Javascript syntax to complete it, but for Flutter development, What is the syntax for Dart? Often, you have to go back to the Dart document to find the syntax. Fifteen examples of JavaScript code and their Dart counterparts are shared here.

1. JSON. Stringify and JsonEncoder () to convert

In JavaScript, to convert an object to a JSON string, you can use:

  JSONStringify (yourObject)Copy the code

In Dart, simply import ‘Dart :convert’ to use:

  import 'dart:convert';
  
  JsonEncoder().convert(yourObject)
Copy the code

2. JSON. Parse and JsonDecoder () to convert

To convert JSON to an object in JavaScript, use the following code:

  JSON.parse(yourJson)
Copy the code

In Dart, simply import ‘Dart :convert’ to use:

  import 'dart:convert';
  
  JsonDecoder().convert(yourJson)
Copy the code

3. Array. A push and list. Add

In JavaScript, to add ‘hello’ to an array, you can use

  array.push('hello')
Copy the code

In Dart, used this way,

  list.add('hello')
Copy the code

4. Array. Splice and list. Sublist

In JavaScript, the array removes the first element (index 0)

  array.splice(0.1)
Copy the code

In Dart, the new list is returned within the index range (1 and 3)

  list.sublist(1.3)
Copy the code

5. Array. Splice and list the removeAt

Array.splice is the same as #4

In Dart, delete the value indexed to itemIndex

  list.removeAt(itemIndex)
Copy the code

Array. length > 0 and list.isNotEmpty

Check if the array is empty

  array.length > 0
Copy the code

Dart, here’s the thing

  list.isNotEmpty
Copy the code

Array. length === 0 and list.isEmpty

Check if the array is empty

  array.length === 0
Copy the code

Dart, here’s the thing

  list.isEmpty
Copy the code

8. ParseInt and int. Parse

Convert a string to an integer in JavaScript

  parseInt('123')
Copy the code

In Dart, this is true

  int.parse('123')
Copy the code

9. The parseFloat and double. Parse

Double-precision floating-point numbers are used as follows:

  parseFloat('1.23')
Copy the code

In Dart, this is true

  double.parse('1.23')
Copy the code

10. Array. Some and list. Any

In JavaScript, the array.some() method tests whether at least one element in an array passes the provided function test. It returns a Boolean. For example, if you want to see if the array [1, 2, 3, 4, 5, 6, 7] has any numbers greater than 5:

  var some = array.some(x= >x > 5)
Copy the code

In Dart, it looks like this,

  array.any((x) = >x > 5);
Copy the code

11. Array. FindIndex and list. IndexWhere

Javascript’s array.findIndex() method returns the index of the first element in the array that satisfies the provided test function. Returns -1 if no corresponding element is found. Dart uses list.indexWhere, but the usage is different

var notes = ['do'.'re'.'mi'.'re'];
// JavaScript
notes.findIndex(x= >x.indexOf("r") > -1) / / 1
// Dart
notes.indexWhere((note) = > note.startsWith('r')); / / 1
Copy the code

Tip in Dart, you can also pass a parameter after the function to provide the starting index. As follows:

notes.indexWhere((note) = > note.startsWith('r'), 2 ); / / 3
Copy the code

12. Array. IndexOf () and list. The indexOf ()

In JavaScript and Dart, indexOf is a method used to find the location of an index in an array or list. If not, -1 is returned

var author = ['Edgar Allen Poe'.'JK Rowling'.'Ernest Hemingway'] 
author.indexOf('JK Rowling') / / 1
Copy the code

13. toString

In JavaScript, toString() is used to convert a value of another data type to a string. It works with integers, floating point numbers, Booleans, arrays, and objects. For objects we get the following result:

var x = {id: 1}
console.log(x.toString())
=> '[object Object]'
Copy the code

However, there will be certain differences between the two, as follows:

 // Dart
 String test = 1.toString() / / '1'
 // JavaScript
 var x = 1.toString() // Uncaught syntax errors: invalid or unexpected tags
Copy the code

14. Array. Every and list. Every

The every() method tests whether all elements in an array pass the test of a given function. It returns a Boolean value. Usage is similar to array.some and list.any above

15. Map, forEach, filter, find, etc

With the exception of some differences in syntax, all of the above methods are shared between JavaScript and Dart. See the following [1, 2, 3, 4, 5] when map is used for arrays of test equals. Javascript:

  // Other similar
  test.map(x= >x*2)
Copy the code

Dart:

est.map((x) = >x*2)
Copy the code

If there’s anything else you want to add? Let me know in the comments!