跳转到内容
代码片段·免费

Dart 和 Flutter 代码片段

20 个 Dart 和 Flutter 代码片段:initState 与 dispose、Scaffold、FutureBuilder 与 StreamBuilder、ListView.builder、页面跳转、对话框和 SnackBar、带 copyWith 的 JSON 模型,以及单元测试和 widget 测试。

免费安装。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

20 个中的 5 个

initStateinitst

Set up a State object once, before the first build

@override
void initState() {
  super.initState();
  $0
}

disposedisp

Release controllers when the widget leaves the tree

@override
void dispose() {
  ${1:_controller}.dispose();
  super.dispose();
}

Scaffold with an app barscaff

Screen skeleton with a title bar and a body

Scaffold(
  appBar: AppBar(title: const Text('${1:Title}')),
  body: ${2:const Placeholder()},
)

FutureBuilderfutbuild

Show a spinner, the error or the result of a Future

FutureBuilder<${1:String}>(
  future: ${2:_future},
  builder: (context, snapshot) {
    if (snapshot.connectionState != ConnectionState.done) {
      return const CircularProgressIndicator();
    }
    if (snapshot.hasError) {
      return Text(snapshot.error.toString());
    }
    return Text(${3:snapshot.data ?? ''});
  },
)

StreamBuilderstreambld

Rebuild every time a Stream emits a value

StreamBuilder<${1:int}>(
  stream: ${2:_counter},
  builder: (context, snapshot) {
    if (!snapshot.hasData) {
      return const CircularProgressIndicator();
    }
    return Text(${3:snapshot.data.toString()});
  },
)

1.0.0 的变更

首个版本。

相关