Skip to content

Instantly share code, notes, and snippets.

View tomaszpolanski's full-sized avatar
💭
Fluttering

Tomek Polański tomaszpolanski

💭
Fluttering
View GitHub Profile
@tomaszpolanski
tomaszpolanski / main_safe.dart
Last active November 9, 2021 17:57
Null safe
final String? text;
@override
Widget build(BuildContext context) {
final _text = text; // Assign field to local final variable
return Column(
children: [
if (_text != null) Text(_text), // The `!` is not longer needed
],
);
@tomaszpolanski
tomaszpolanski / fields_error.dart
Last active November 7, 2021 11:31
Error when accessing
void main() {
final A example = B();
if (example.text != null) {
print(example.text!.length); // Throws null reference exception
}
}
@tomaszpolanski
tomaszpolanski / fields.dart
Created November 7, 2021 11:13
Not truly final fields
class A {
A(this.text);
final String? text; // Final field
}
class B implements A {
bool _first = true;
@override
@tomaszpolanski
tomaszpolanski / main_non_safe.dart
Created November 7, 2021 11:11
Not null safe code
final String? text;
@override
Widget build(BuildContext context) {
return Column(
children: [
if (text != null) Text(text!), // Won't compile without `!` in `text!`
],
);
}
@tomaszpolanski
tomaszpolanski / fields.dart
Last active November 7, 2021 10:33
Issue With dart null safety 1
class A {
A(this.text);
final String? text; // Final field
}
class B implements A {
bool _first = true;
@override
### Keybase proof
I hereby claim:
* I am tomaszpolanski on github.
* I am tomekpolanski (https://keybase.io/tomekpolanski) on keybase.
* I have a public key ASCKUlioKVilX-ZWBBM_SSeGjxkVO-nssaEU9BDiqg8bSQo
To claim this, I am signing this object:
import 'package:integration_test/integration_test.dart';
import 'route_test.dart' as route;
import 'simple_test.dart' as simple;
import 'subfolder/sub_test.dart' as subfolder_sub;
import 'testing_test.dart' as testing;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
route.main();
final finder = find.text('Done');
for (int i = 0; i < 10; i++) {
await tester.pump(const Duration(seconds: 1));
if (findsOneWidget.matches(finder, {})) {
break;
}
}
abstract class TimeProvider {
Future<void> delayed(Duration duration);
}
class TimeProviderImpl implements TimeProvider {
@override
Future<void> delayed(Duration duration) => Future<void>.delayed(duration);
}
class MockTimeProvider implements TimeProvider {
@tomaszpolanski
tomaszpolanski / time_test.dart
Created April 5, 2021 13:00
Testing time with Widget and Driver tests
testWidgets('time test', (tester) async {
const done = Key('done');
const pending = Key('pending');
await tester.pumpWidget(
FutureBuilder(
future: Future<void>.delayed(const Duration(seconds: 10)),
builder: (context, sn) {
return sn.connectionState == ConnectionState.done
? const SizedBox(key: done)
: const SizedBox(key: pending);