Created
August 23, 2023 15:24
-
-
Save chimon2000/ad9728712f85745e07d3459031513e93 to your computer and use it in GitHub Desktop.
Riverpod snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Async Details Screen": { | |
"prefix": "ads", | |
"body": [ | |
"import 'package:flutter/material.dart';", | |
"import 'package:flutter_riverpod/flutter_riverpod.dart';", | |
"", | |
"class ${1:Example}Screen extends ConsumerWidget {", | |
" const ${1:Example}Screen({", | |
" super.key,", | |
" required this.id,", | |
" });", | |
"", | |
" final String id;", | |
"", | |
" @override", | |
" Widget build(BuildContext context, WidgetRef ref) {", | |
" final ${2:exampleState}Async = ref.watch(${exampleProvider}(id));", | |
" return Scaffold(", | |
" body: ${2:exampleState}Async.when(", | |
" data: ${1:Example}DataView.new,", | |
" error: ${1:Example}ErrorView.new,", | |
" loading: ${1:Example}LoadingView.new,", | |
" ),", | |
" );", | |
" }", | |
"}", | |
"", | |
"class ${1:Example}LoadingView extends StatelessWidget {", | |
" @visibleForTesting", | |
" const ${1:Example}LoadingView({super.key});", | |
"", | |
" @override", | |
" Widget build(BuildContext context) {", | |
" return Container();", | |
" }", | |
"}", | |
"", | |
"class ${1:Example}ErrorView extends StatelessWidget {", | |
" @visibleForTesting", | |
" const ${1:Example}ErrorView(this.error, this.stackTrace, {super.key});", | |
"", | |
" final Object error;", | |
" final StackTrace stackTrace;", | |
"", | |
" @override", | |
" Widget build(BuildContext context) {", | |
" return Container();", | |
" }", | |
"}", | |
"", | |
"class ${1:Example}DataView extends StatelessWidget {", | |
" @visibleForTesting", | |
" const ${1:Example}DataView(this.data, {super.key});", | |
"", | |
" final ${1:Example}State data;", | |
"", | |
" @override", | |
" Widget build(BuildContext context) {", | |
" return Container();", | |
" }", | |
"}", | |
"", | |
"" | |
], | |
"description": "Async Details Screen" | |
}, | |
"Async List Screen": { | |
"prefix": "als", | |
"body": [ | |
"import 'package:flutter/material.dart';", | |
"import 'package:flutter_riverpod/flutter_riverpod.dart';", | |
"", | |
"class ${1:Examples}Screen extends ConsumerWidget {", | |
" const ${1:Examples}Screen({", | |
" super.key,", | |
" required this.id,", | |
" });", | |
"", | |
" final String id;", | |
"", | |
" @override", | |
" Widget build(BuildContext context, WidgetRef ref) {", | |
" final ${2:exampleStates}Async = ref.watch(${2:exampleStates}Provider);", | |
"", | |
" return Scaffold(", | |
" body: ${2:exampleStates}Async.when(", | |
" data: ${1:Examples}DataView.new,", | |
" error: ${1:Examples}ErrorView.new,", | |
" loading: ${1:Examples}LoadingView.new,", | |
" ),", | |
" );", | |
" }", | |
"}", | |
"", | |
"class ${1:Examples}LoadingView extends StatelessWidget {", | |
" const ${1:Examples}LoadingView({super.key});", | |
"", | |
" @override", | |
" Widget build(BuildContext context) {", | |
" return Container();", | |
" }", | |
"}", | |
"", | |
"class ${1:Examples}ErrorView extends StatelessWidget {", | |
" @visibleForTesting", | |
" const EventDetailErrorView(this.error, this.stackTrace, {super.key});", | |
"", | |
" final Object error;", | |
" final StackTrace stackTrace;", | |
"", | |
" @override", | |
" Widget build(BuildContext context) {", | |
" return Container();", | |
" }", | |
"}", | |
"", | |
"class ${1:Examples}DataView extends StatelessWidget {", | |
" const ${1:Examples}DataView(this.data, {super.key});", | |
"", | |
" final List<${1:Example}> data;", | |
"", | |
" @override", | |
" Widget build(BuildContext context) {", | |
" return Container();", | |
" }", | |
"}", | |
"" | |
], | |
"description": "Async List Screen" | |
}, | |
"Async Screen Test": { | |
"prefix": "ast", | |
"body": [ | |
"import 'dart:async';", | |
"", | |
"import 'package:flutter/material.dart';", | |
"import 'package:flutter_riverpod/flutter_riverpod.dart';", | |
"import 'package:flutter_test/flutter_test.dart';", | |
"", | |
"void main() {", | |
" group('$${1:Example}Screen', () {", | |
" testWidgets('it displays data on success', (tester) async {", | |
" await tester.pumpWidget(", | |
" ProviderScope(", | |
" overrides: [", | |
" ${2:store}StoreProvider.overrideWith((ref) => const ${1:Example}State())", | |
" ],", | |
" child: const MaterialApp(home: ${1:Example}Screen()),", | |
" ),", | |
" );", | |
"", | |
" final result = find.byType(${1:Example}DataView);", | |
" expect(result, findsOneWidget);", | |
" });", | |
"", | |
" testWidgets('it displays data on loading', (tester) async {", | |
" await tester.pumpWidget(", | |
" ProviderScope(", | |
" overrides: [", | |
" ${2:store}StoreProvider", | |
" .overrideWith((ref) => Completer<${1:Example}State>().future)", | |
" ],", | |
" child: const MaterialApp(home: ${1:Example}Screen()),", | |
" ),", | |
" );", | |
"", | |
" final result = find.byType(${1:Example}LoadingView);", | |
" expect(result, findsOneWidget);", | |
" });", | |
"", | |
" testWidgets('it displays error on failure', (tester) async {", | |
" await tester.pumpWidget(", | |
" ProviderScope(", | |
" overrides: [", | |
" ${2:store}StoreProvider.overrideWith((ref) => throw 'error')", | |
" ],", | |
" child: const MaterialApp(home: ${1:Example}Screen()),", | |
" ),", | |
" );", | |
"", | |
" final result = find.byType(${1:Example}ErrorView);", | |
" expect(result, findsOneWidget);", | |
" });", | |
" });", | |
"}", | |
"" | |
], | |
"description": "Async Screen Test" | |
}, | |
"Async Details Screen Test": { | |
"prefix": "adst", | |
"body": [ | |
"import 'dart:async';", | |
"", | |
"import 'package:flutter/material.dart';", | |
"import 'package:flutter_riverpod/flutter_riverpod.dart';", | |
"import 'package:flutter_test/flutter_test.dart';", | |
"", | |
"void main() {", | |
" const ${Arg} id = ${arg};", | |
" group('$${1:Example}Screen', () {", | |
" testWidgets('it displays data on success', (tester) async {", | |
" await tester.pumpWidget(", | |
" ProviderScope(", | |
" overrides: [", | |
" ${2:example}StoreProvider.overrideWith(() => ${1:Example}StoreStub(${1:Example}State()))", | |
" ],", | |
" child: const MaterialApp(home: ${1:Example}Screen(id: id)),", | |
" ),", | |
" );", | |
"", | |
" final result = find.byType(${1:Example}DataView);", | |
" expect(result, findsOneWidget);", | |
" });", | |
"", | |
" testWidgets('it displays data on loading', (tester) async {", | |
" await tester.pumpWidget(", | |
" ProviderScope(", | |
" overrides: [", | |
" ${2:example}StoreProvider", | |
" .overrideWith(() => ${1:Example}StoreStub(Completer<${1:Example}State>().future))", | |
" ],", | |
" child: const MaterialApp(home: ${1:Example}Screen(id: id)),", | |
" ),", | |
" );", | |
"", | |
" final result = find.byType(${1:Example}LoadingView);", | |
" expect(result, findsOneWidget);", | |
" });", | |
"", | |
" testWidgets('it displays error on failure', (tester) async {", | |
" await tester.pumpWidget(", | |
" ProviderScope(", | |
" overrides: [", | |
" ${2:example}StoreProvider.overrideWith(() => throw 'error')", | |
" ],", | |
" child: const MaterialApp(home: ${1:Example}Screen(id: id)),", | |
" ),", | |
" );", | |
"", | |
" final result = find.byType(${1:Example}ErrorView);", | |
" expect(result, findsOneWidget);", | |
" });", | |
" });", | |
"}", | |
"", | |
"class ${1:Example}StoreStub extends ${1:Example}Store {", | |
" ${1:Example}StoreStub(this._initialValue) : super();", | |
"", | |
" final FutureOr<${1:Example}State> _initialValue;", | |
"", | |
" @override", | |
" FutureOr<${1:Example}State> build(${Arg} arg) {", | |
" return _initialValue;", | |
" }", | |
"}" | |
], | |
"description": "Async Notifier Screen Test" | |
}, | |
"Async Notifier Store": { | |
"prefix": "ans", | |
"body": [ | |
"import 'dart:async';", | |
"", | |
"import 'package:riverpod/riverpod.dart';", | |
"import 'package:equatable/equatable.dart';", | |
"", | |
"final ${example}StoreProvider = AsyncNotifierProvider<${Example}Store, ${Example}State>(${Example}Store.new);", | |
"", | |
"class ${Example}Store extends AsyncNotifier<${Example}State> {", | |
" ${Example}Store();", | |
"", | |
" @override", | |
" FutureOr<${Example}State> build() async {", | |
" return ${Example}State();", | |
" }", | |
"}", | |
"", | |
"class ${Example}State extends Equatable {", | |
" @override", | |
" // TODO: implement props", | |
" List<Object?> get props => throw UnimplementedError();", | |
"}", | |
"" | |
], | |
"description": "Async Notifier Store" | |
}, | |
"Family Async Notifier Store": { | |
"prefix": "fans", | |
"body": [ | |
"import 'dart:async';", | |
"", | |
"import 'package:riverpod/riverpod.dart';", | |
"import 'package:equatable/equatable.dart';", | |
"", | |
"final ${example}StoreProvider = AsyncNotifierProviderFamily<${Example}Store, ${Example}State, ${Arg}>(${Example}Store.new);", | |
"", | |
"class ${Example}Store extends FamilyAsyncNotifier<${Example}State, ${Arg}> {", | |
" ${Example}Store();", | |
"", | |
" @override", | |
" FutureOr<${Example}State> build(${Arg} arg) async {", | |
" return ${Example}State();", | |
" }", | |
"}", | |
"", | |
"class ${Example}State extends Equatable {", | |
" @override", | |
" // TODO: implement props", | |
" List<Object?> get props => throw UnimplementedError();", | |
"}", | |
"" | |
], | |
"description": "Family Async Notifier Store" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment