[Flutter] Dart's data type list&Map (array and dictionary)

1. Write in front

Introduced in the previous articleDartString in primitive data type string, then continue to study nowDartArrays in the base syntax of (list) and a dictionary (Map).
Flutter

[Flutter] Apple Mac computer configuration flutter development environment

[Flutter] Android Studio installs a third-party emulator - Netease MuMu

[Flutter] Project running error Failed to find Build Tools revision 29.0.2

【Flutter】flutter doctor 报错Android license status unknown. Run `flutter doctor --android-licenses‘

[Flutter] How to create a new project and run your first flutter project

[Flutter] Basic use of var, final and const in Dart

[Flutter] num of Dart data type

[Flutter] String of Dart data type

2. list list (array)

existdartmiddlelistlist, which is equivalent toOCmiddleArrayarray, also divided intovariableandimmutabletwo kinds.

2.1 Variable list

Not much to say, go directly to the code, as follows:

void  main ( ) {

  listTest();

} 
void  listTest ( ) { 
  // create 
  List directly from list a =  [ "1" , 2 , "3.0" , 4.0 ] ; 
  print ( a ) ; 
// var create 
  var list =  [ 1 , 2 , "zjp" , 3.0 ] ; 
  print ( list ) ;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

The default is variable, and data of different data types can be added to the list, which is very convenient. The running results are as follows:
operation result

2.1.1 Get the specified subscript data

//Get the specified subscript data directly 
  print ( list [ 3 ] ) ; 
  // Directly modify the specified subscript data 
  list [ 3 ]  =  "reno" ;
  • 1
  • 2
  • 3
  • 4

2.1.2 Insert data method

 //insert list 
  .insert ( 1 , "hellow" ) ; //list.insert(index, element ) 
  • 1
  • 2

2.1.3 Method of deleting data

 // remove 
  list.remove ( 1 ) ; print ( list ) ; _
  
  • 1
  • 2
  • 3

2.1.4 Clear data method

list.clear();
  • 1

The test results are as follows:
code test results

2.1.5 Sorting method

operation result

More ways to checklist.dart, with notes and instructions in it, andiOSThe way to view a class in it is the same,command+mouse clickJust go.

2.2 Immutable lists

immutablelistjust prependconstThat's it, as follows:
Test Results
listprependedconstIt is immutable, and the data inside cannot be modified, otherwise an error will be reported.

3. Map (dictionary)

3.1 Mutable Map

existdartmiddlemapis a key-value pair, that isOCmiddledictionary dictionarysame meaning.

  • create a map
void mapTest(){
  Map a = {"a":1,"b":2};
  print(a);

  var a1 = {"a1":1,"a2":2};
  print(a1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • The results are as follows:

operation result

3.1.1 map value, change value

 print(a["a"]);//取值
  a["a"] = "aa";//改值
  print(a["a"]);
  • 1
  • 2
  • 3

3.1.2 map length

 //map length 
  print ( a . length ) ;
  • 1
  • 2

3.1.3 map all keys

 // get all keys 
  print ( a . keys ) ;
  • 1
  • 2

3.1.4 map all values

  // get all values 
  ​​print ( a . values ​​) ;
  • 1
  • 2
  • The test results are as follows:

Test Results

3.2 Cannot change map

Immutable is preceded byconst, immutablemapkey-value pairs cannot be modified, andlistThe same modification will report an error.

error message

  • list can be converted to map
print ( b . asMap ( ) ) ; //list is converted to map
  • 1
  • The result is as follows:

operation result
More methods can be viewed in specific classes.

4. Write at the back

Follow me, more content will continue to be output

🌹 If you like it, give it a like👍🌹

🌹 If you think you have something to gain, you can come to a wave of favorites + follow, so as not to find me next time😁🌹

🌹Welcome everyone to leave a message to exchange, criticize and correct,ForwardPlease indicate the source, thank you for your support! 🌹

Related: [Flutter] Dart's data type list&Map (array and dictionary)