• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

dart 练习

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

code

import 'dart:io';
import 'dart:core';
import 'dart:collection';
import 'dart:async';

void main() {
  var lst = new List(); 
  lst.add(3); 
  lst.add(4); 
  print(lst);

  print("-----------");

  List<int> nameOfTest = List(3); 
  nameOfTest[0] = 1; 
  nameOfTest[1] = 2; 
  nameOfTest[2] = 3;
  print(nameOfTest);

  print("-----------");

  for(int element in nameOfTest){
    print(element);
  }
  print("-----------");

  nameOfTest.forEach((v) => print('${v}'));

  print("-----------");

  for(int i = 0; i < nameOfTest.length; i++){
    print(nameOfTest[i]);
  }

  print("-----------");

  List<String> names = List(); 
  names.add("Mana"); 
  names.add("Babu");
  names.add("Gopal"); 
  names.add("Pota");
  print(names);

  print("-----------");


  var number1 = 1;
  var number2 = 1; 
  while(number2 < 50){
    print(number2);
    number2 += number1;
    number1 = number2 - number1;
  }
  print("Separator line: =============");
  var fibonacciNumbers = [1, 2, 3, 5, 8, 13, 21, 34];
  print(fibonacciNumbers.take(3).toList()); 
  print("Separator line: ============="); 
  print(fibonacciNumbers.skip(5).toList()); 
  print("Separator line: ============="); 
  print(fibonacciNumbers.skip(2).contains(5)); 
  print("Separator line: =============");
  print(fibonacciNumbers.take(3).skip(2).take(1).toList()); 
  print("Separator line: =============");
  var clonedFibonacciNumbers = List.from(fibonacciNumbers); 
  print("Cloned list: $clonedFibonacciNumbers");

  print("-----------");


  var names1 = ["John", "Robert", "Smith", "Peter"];
  names1.forEach((name) => print(name));

  print("-----------");

  var mappedNames = names.map((name) => "Absconding $name").toList();
  print(mappedNames);

  print("-----------");

  Set<String> countries = Set.from(['India', 'England', 'US']); 
  for(String element in countries){
    print(element);
  }
  Set<int> numbers = Set.from([1, 45, 58]);
    for(int element in numbers){
    print(element);
  }
  Set<int> moreNumbers = Set();
  moreNumbers.add(178);
  moreNumbers.add(568);
  moreNumbers.add(569);
  print(moreNumbers);

  print("-----------");

  countries.forEach((v) => print('haha_${v}'));
  for(int element in moreNumbers){
    if(moreNumbers.lookup(element) == 568){ 
      print(element);
      break;
    }
    print("xixi"+(moreNumbers.lookup(element)).toString()); 
  }

  print("-----------");


  var fruitCollection = {'Mango', 'Apple', 'Jack fruit'}; 
  print(fruitCollection.lookup('Something Else'));
  //lists
  List fruitCollections = ['Mango', 'Apple', 'Jack fruit']; 
  var myIntegers = [1, 2, 3, '4monkey']; 
  print(myIntegers[3]);
  print(fruitCollections[0]);

  print("-----------");

  Map<String, String> countries1 = Map(); 
  countries1['India'] = "Asia"; 
  countries1["Germany"] = "Europe"; 
  countries1["France"] = "Europe"; 
  countries1["Brazil"] = "South America";
  for(var key in countries1.keys){
    print("Country's name: $key"); 
  }
  for(String value in countries1.values){
    print("Continent's name: $value"); 
  }

  print("-----------");

  countries1.forEach((key, value) => print("Country: $key and Continent: $value"));
  if(countries1.containsKey("Germany")){
    countries1.update("Germany", (value) => "European Union"); 
    print("Updated country Germany.");
    countries1.forEach((key, value) => print("Country: $key and Continent: $value"));
  }

  print("-----------");

  countries1.remove("Brazil");
  countries1.forEach((key, value) => print("Country: $key and Continent: $value"));
  
  print("-----------");

  Map<String, int> telephoneNumbersOfCustomers = {"John" : 1234, "Mac" : 7534, "Molly" : 8934, "Plywod" : 1275, "Hagudu" : 2534};
  telephoneNumbersOfCustomers.forEach((key, value) =>print("Customer: $key and Contact Number: $value"));

  print("-----------");

  for(var key in countries1.keys){ 
    print("Country's name: $key");
  } 
  
  print("-----------");

  for(String value in countries1.values){
    print("Continent's name: $value"); 
  }

  countries1.forEach((key, value) => print("Country: $key and Continent: $value"));

  print("-----------");


  List<Map<String, dynamic>> users = [{ "name": "Peters", "age": 18 }, { "name": "Miras", "age": 20 }, { "name": "Jasons", "age": 22 }];
  var is18AndOver = users.every((user) => user["age"] > 20); 
  print(is18AndOver);
  var is19AndOver = users.every((user) => user["age"] >= 18); 
  print(is19AndOver);

  var hasNamesWithLetterA = users.every((user) => user.toString().startsWith("A")); 
  print(hasNamesWithLetterA);
  var hasNamesWithLetterB = users.every((user) => user["name"].endsWith("s")); 
  print(hasNamesWithLetterB);

  users.forEach((user) => print(user));
  print(users);

  var overTwentyOne = users.where((user) => user["age"] > 21); 
  print(overTwentyOne);
  print(overTwentyOne.length);

  var underEighteen = users.singleWhere((user) => user["age"] < 18, orElse: () => null);
  print(underEighteen);

  print("-----------");

  List nums = [1, 2, 3, 4, 5, 6]; 
  print(nums.take(0));
  print(nums.take(5));

  print("-----------");

  print(nums.takeWhile((element) => element < 5));

  print("-----------");

  print(nums.skip(4));

  print("-----------");

  print(nums.skipWhile((element) => element <= 3));

  print("-----------");

  print(nums.toList(growable:false));

  print("-----------");

  List nums111 = [1, 2, 1]; 
  print(nums111.toSet());

  print("-----------");

  nums = [1, 2, 3];
  print(nums.reduce((sum, element) => sum + element));

  print("-----------");

  nums = [1, 2, 3]; 
  print(nums.join(' - '));

  print("-----------");

  List<int> list44 = [1, 2, 3, 4, 5];
  int result10 = list44.fold(2, (a,element)=>(a+element));
  print(result10);

  int result11 = list44.fold(2, (a,element)=>(a*element));
  print(result11);

  print("-----------");


  Iterable generated = new Iterable.generate(4, (count) => "int $count");
  print(generated);

  generated = new Iterable.generate(4); 
  print(generated);


  print("-----------");

  nums = [1, 2, 3];
  print('Get Iterable for $nums'); 
  ///返回奇数
  Iterable iterable = nums.where((i) {
    print('Fetched $i');
    return i.isOdd; 
  });
  print('Start fetching'); 
  iterable.forEach((i) {
    print("Received $i");
  });

  print("-----------");


  Queue myQueue = new Queue();
  print("Default implementation ${myQueue.runtimeType}");
  myQueue.add("Sanjib");
  myQueue.add(54);
  myQueue.add("Howrah"); 
  myQueue.add("[email protected]"); 
  for(var allTheValues in myQueue){
      print(allTheValues);
  }
  print("----------");
  print("We are removing the first element ${myQueue.elementAt(0)}.");
  myQueue.removeFirst();
  for(var allTheValues in myQueue){
    print(allTheValues);
  }
  print("----------");
  print("We are removing the last element ${myQueue. elementAt(2 )}.");
  myQueue.removeLast();
  for(var allTheValues in myQueue){
    print(allTheValues);
  }

  print("-----------");

  var withoutArgument = CallableClassWithoutArgument();
  var withArgument = CallableClassWithArgument(); 
  withoutArgument();
  print(withArgument("John Smith")); 
  print(withArgument.call("Calling John Smith"));

  print("-----------");

  var John = Person();
  John.name = "John Smith";
  String name = John.name;
  String msgAndName = John("Hi John how are you?", name); 
  print(msgAndName);

  print("-----------");

  Future<String> checkingNewsApp() {
  Future<String> delayingTenSeconds = Future.delayed(Duration(seconds: 10), (){
    return "The latest headlines are displayed here after 10 seconds."; });
    return delayingTenSeconds;
  }
  void displayNews() {
    Future<String> displayingNewsHeadlines = checkingNewsApp();
    displayingNewsHeadlines.then((displayString){
      print("Displaying news headlines here: $displayingNewsHeadlines");
    }); 
  }

  print("The main UI thread is starting on here.");
  print("Now it will take 10 seconds to display news headlines.");
  displayNews();
  print("The main UI thread ends.");
  
  print("-----------");



  Future checkVersion() async {
    var version = await checkVersion(); // Do something with version
    try {
      return version;
    } 
    catch (e) {
      return e; 
    }
  }
  print(checkVersion());

}

class CallableClassWithoutArgument {
  String output = "Callable class"; 
  void call() {
    print(output);
  }
}

class CallableClassWithArgument { 
  call(String name) => "$name";
}

class Person{
  String name;
  String call(String message, [name]){
    return "This message: '$message', has been passed to the person $name."; 
  }
}

 

 

 

 

 

 

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Dart异步编程之Future发布时间:2022-07-13
下一篇:
Flutter &amp; Dart 安装在window系统发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap