Skip to content

Instantly share code, notes, and snippets.

View tomalabaster's full-sized avatar

Tom Alabaster tomalabaster

View GitHub Profile
class PartialStateYieldingScreenBloc extends BaseBloc<
PartialStateYieldingScreenEvent, PartialStateYieldingScreenState> {
int _property1;
int _property2;
int _property3;
@override
PartialStateYieldingScreenState get initialState {
this._property1 = 0;
this._property2 = 0;
import 'package:flutter_app_template/blocs/base_bloc.dart';
import 'package:flutter_app_template/blocs/state_yielding_example_blocs/full_state_yielding_screen_bloc/full_state_yielding_screen_event.dart';
import 'package:flutter_app_template/blocs/state_yielding_example_blocs/full_state_yielding_screen_bloc/full_state_yielding_screen_state.dart';
class FullStateYieldingScreenBloc extends BaseBloc<FullStateYieldingScreenEvent,
FullStateYieldingScreenState> {
int _property1;
int _property2;
int _property3;
@tomalabaster
tomalabaster / init_in_widget.dart
Last active March 1, 2020 01:07
BAD: Async init done in a widget
class _MyAppState extends State<MyApp> {
Database _database;
@override
void initState() {
super.initState();
asyncInitState();
}
@tomalabaster
tomalabaster / init_in_main.dart
Created March 1, 2020 01:06
GOOD: Async init done in main method
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, 'example.db');
var database = await openDatabase(path, version: 1, onCreate: (db, version) async {
await db.execute('''CREATE TABLE MyTable (
id INTEGER PRIMARY KEY,
name TEXT)''');
@tomalabaster
tomalabaster / wait_for_state_from_bloc.dart
Created January 3, 2021 20:32
A utility method for waiting for a bloc to yield a specific state.
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';
class BlocClosedWithoutYieldingStateException implements Exception {}
Future<T> waitForStateFromBloc<T>({
@required Bloc bloc,
}) {
@tomalabaster
tomalabaster / main.dart
Created November 28, 2023 18:58
Counter example
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
@tomalabaster
tomalabaster / main.dart
Created February 4, 2024 01:01
PopScope.onPopInvokedBehaviour
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override