Skip to content
Snippets·Free

Dart and Flutter snippets

20 Dart and Flutter snippets: initState and dispose, Scaffold, FutureBuilder and StreamBuilder, ListView.builder, navigation, dialogs and SnackBars, a JSON model with copyWith, and unit and widget tests.

Free to install.

If PocketCode is installed on your Android device, this opens the page in the app.

Preview

5 of 20

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()});
  },
)

Changes in 1.0.0

First version.

Related