Dart web app

发布时间 2023-10-13 15:33:08作者: vx_guanchaoguo0

安装 dart

# 设置 brew 源
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

# 安装
brew install dart-sdk

# 查看版本
dart --version

# 查看安装目录
arch -arm64  brew info dart-sdk     

安装

  • 安装 webstrom
  • 安装 Dart 插件

项目

# 创建项目
dart create -t web web_app
  • index.html
<h2>A Simple To-Do List</h2>

<p>Things to do:</p>

<ul id="todolist">
</ul>
  • main.dart
import 'dart:html';

Iterable<String> thingsTodo() sync* {
  const actions = ['Walk', 'Wash', 'Feed'];
  const pets = ['cats', 'dogs'];

  for (final action in actions) {
    for (final pet in pets) {
      if (pet != 'cats' || action == 'Feed') {
        yield '$action the $pet';
      }
    }
  }
}

void addTodoItem(String item) {
  final listElement = LIElement()..text = item;
  todoList.children.add(listElement);
}

final UListElement todoList = querySelector('#todolist') as UListElement;

void main() {
  thingsTodo().forEach(addTodoItem);
}

启动项目

  • run->edit->dart web