1. Home
  2. /
  3. Dart
  4. /
  5. Counter with History

Counter with History - Dart Typing CST Test

Loading…

Counter with History — Dart Code

A console-based counter that logs each increment/decrement action in a history list.

void main() {
	int count = 0;
	List<String> history = [];

	void updateUI() {
		print('Counter: $count');
		print('History: ${history.join(', ')}');
	}

	void increment() {
		count++;
		history.add('Incremented to $count');
		updateUI();
	}

	void decrement() {
		count--;
		history.add('Decremented to $count');
		updateUI();
	}

	void reset() {
		count = 0;
		history.clear();
		history.add('Counter reset');
		updateUI();
	}

	updateUI();
	increment();
	increment();
	decrement();
	reset();
}

Dart Language Guide

Dart is a modern, object-oriented programming language developed by Google, optimized for building web, server, and mobile applications, particularly for Flutter. It emphasizes performance, strong typing, and productive development.

Primary Use Cases

  • ▸Mobile app development with Flutter
  • ▸Web applications and progressive web apps (PWAs)
  • ▸Server-side applications with Dart VM
  • ▸Command-line tools and scripts
  • ▸Cross-platform desktop applications

Notable Features

  • ▸Strongly typed with sound null safety
  • ▸Ahead-of-time (AOT) and just-in-time (JIT) compilation
  • ▸Hot reload for fast development cycles
  • ▸Rich standard library and asynchronous support
  • ▸Interoperability with JavaScript for web development

Origin & Creator

Created in 2011 by Google, designed by Lars Bak and Kasper Lund.

Industrial Note

Dart is specialized for cross-platform development, especially for Flutter, enabling single codebases for mobile, web, and desktop applications.

Quick Explain

  • ▸Dart allows developers to write fast, maintainable, and expressive code.
  • ▸It supports both just-in-time (JIT) compilation for development and ahead-of-time (AOT) compilation for production.
  • ▸Dart combines object-oriented, functional, and reactive programming paradigms.

Core Features

  • ▸Object-oriented with classes, mixins, and interfaces
  • ▸Asynchronous programming with Futures and Streams
  • ▸Optional static typing with type inference
  • ▸Functional programming constructs like first-class functions
  • ▸Comprehensive libraries for I/O, collections, and math

Learning Path

  • ▸Learn Dart basics: variables, types, control flow
  • ▸Understand functions and classes
  • ▸Master asynchronous programming with Futures and Streams
  • ▸Practice Flutter app development
  • ▸Build full-stack Dart applications

Practical Examples

  • ▸Flutter mobile app with hot reload
  • ▸Command-line data processing tool
  • ▸Server-side API with Dart shelf
  • ▸Web app compiled to JavaScript
  • ▸Desktop app using Flutter for Windows/macOS/Linux

Comparisons

  • ▸Stronger typing and better tooling than JavaScript
  • ▸Faster hot reload cycles than native development
  • ▸Smaller ecosystem than Java or Python
  • ▸Better performance in Flutter apps than other cross-platform tools
  • ▸Syntax similar to C-style languages, easy to learn for Java/C# developers

Strengths

  • ▸Fast compilation and runtime performance
  • ▸Seamless cross-platform development with Flutter
  • ▸Strong tooling support and IDE integration
  • ▸Modern syntax and developer-friendly features
  • ▸Large ecosystem and package management via pub.dev

Limitations

  • ▸Relatively smaller ecosystem compared to JavaScript or Python
  • ▸Limited support for low-level system programming
  • ▸Not as widely adopted outside Flutter ecosystem
  • ▸Web performance depends on JavaScript compilation
  • ▸Smaller talent pool compared to more established languages

When NOT to Use

  • ▸Low-level system programming
  • ▸Projects outside Flutter ecosystem if alternatives exist
  • ▸Heavy computation without proper profiling
  • ▸When small script size is critical for web apps
  • ▸Projects relying on extensive libraries outside Dart ecosystem

Cheat Sheet

  • ▸var x = 10 - variable declaration
  • ▸final y = 20 - immutable variable
  • ▸class MyClass {} - define class
  • ▸Future<void> fetchData() async {} - async function
  • ▸Stream<int> myStream = Stream.fromIterable([1,2,3]) - define stream

FAQ

  • ▸Is Dart object-oriented?
  • ▸Yes, Dart is class-based and object-oriented.
  • ▸Can Dart be used for server-side applications?
  • ▸Yes, using Dart VM and server frameworks like shelf.
  • ▸Does Dart support functional programming?
  • ▸Yes, Dart supports first-class functions and higher-order functions.
  • ▸What is Flutter?
  • ▸Flutter is a UI toolkit built with Dart for cross-platform apps.
  • ▸Does Dart have a garbage collector?
  • ▸Yes, Dart VM manages memory automatically with garbage collection.

30-Day Skill Plan

  • ▸Week 1: Dart syntax and simple scripts
  • ▸Week 2: Object-oriented programming in Dart
  • ▸Week 3: Futures, async/await, and Streams
  • ▸Week 4: Flutter UI and state management
  • ▸Week 5: Advanced Flutter features and deployment

Final Summary

  • ▸Dart is a modern, fast, and versatile language.
  • ▸Supports object-oriented and functional programming paradigms.
  • ▸Designed for cross-platform app development with Flutter.
  • ▸Strong typing with null safety enhances reliability.
  • ▸Ideal for mobile, web, and server-side applications.

Project Structure

  • ▸bin/ - main entry points for CLI apps
  • ▸lib/ - shared libraries and code
  • ▸test/ - unit and integration tests
  • ▸web/ - web app entry for Flutter web
  • ▸pubspec.yaml - package configuration

Monetization

  • ▸Paid apps on App Store/Play Store
  • ▸In-app purchases and subscriptions
  • ▸Flutter plugins and packages on pub.dev
  • ▸Freelance cross-platform app development
  • ▸Enterprise mobile solutions

Productivity Tips

  • ▸Leverage hot reload for fast iteration
  • ▸Use const constructors for immutable widgets
  • ▸Split code into reusable widgets and packages
  • ▸Automate testing and deployment pipelines
  • ▸Use Dart DevTools for profiling and debugging

Basic Concepts

  • ▸Variables, constants, and type system
  • ▸Control flow: if, for, while, switch
  • ▸Functions and first-class functions
  • ▸Classes, objects, and inheritance
  • ▸Asynchronous programming with Futures and Streams

Official Docs

  • ▸https://dart.dev/guides
  • ▸https://api.dart.dev/
  • ▸https://flutter.dev/docs

More Dart Typing Exercises

Dart Theme Toggle and CounterDart Counter with Conditional ThemeDart Counter with Lambda ActionsDart Counter with Max LimitDart Counter with Timer Auto-IncrementDart Counter with User InputDart Counter with Event CallbacksDart Counter with Custom StepDart Counter with Persistent Storage

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher