Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into riverpod-generato…
Browse files Browse the repository at this point in the history
…r/Add-provider-prefix-to-config
  • Loading branch information
ValentinVignal committed Feb 19, 2024
2 parents 19e0f49 + a415d15 commit 0487d76
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 213 deletions.
16 changes: 4 additions & 12 deletions website/docs/essentials/passing_args.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
title: Passing arguments to your requests
version: 2
version: 3
---

import { Link } from "/src/components/Link";
import { AutoSnippet, When } from "/src/components/CodeSnippet";
import noArgProvider from "./passing_args/no_arg_provider";
import family from "!!raw-loader!./passing_args/raw/family.dart";
import codegenFamily from "!!raw-loader!./passing_args/codegen/family.dart";
import family from "./passing_args/family";
import consumerProvider from "!!raw-loader!./passing_args/raw/consumer_provider.dart";
import consumerFamily from "!!raw-loader!./passing_args/raw/consumer_family.dart";
import consumerListFamily from "!!raw-loader!./passing_args/raw/consumer_list_family.dart";
Expand All @@ -33,7 +32,6 @@ As a reminder, previously we defined our provider like this:
<AutoSnippet {...noArgProvider} />

<When codegen={false}>

When not relying on code-generation, we need to tweak the syntax for defining
our providers a bit to support passing arguments. This is done by relying on
the "modifier" called "family".
Expand All @@ -42,22 +40,16 @@ In short, we need to add `.family` after the type of our provider, and
an extra type parameter corresponding to the argument type.
For example, we could update our provider to accept a `String` argument
corresponding to the type of activity desired:

<AutoSnippet raw={family} />

</When>

<When codegen={true}>

To pass parameters to our providers, we can simply add our parameters
on the annotated function itself.
For example, we could update our provider to accept a `String` argument
corresponding to the type of activity desired:

<AutoSnippet raw={codegenFamily} />

</When>

<AutoSnippet {...family} />

:::caution
When passing arguments to providers, it is highly encouraged to
enable "autoDispose" on the provider.
Expand Down
159 changes: 0 additions & 159 deletions website/docs/essentials/passing_args/codegen/family.g.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// ignore_for_file: avoid_print

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../first_request/codegen/activity.dart';

part 'family.g.dart';
part 'codegen.g.dart';

Future<Activity> fetchActivity() => throw UnimplementedError();

/* SNIPPET START */
@riverpod
Expand All @@ -27,3 +31,17 @@ Future<Activity> activity(
final json = jsonDecode(response.body) as Map<String, dynamic>;
return Activity.fromJson(json);
}

@riverpod
class ActivityNotifier2 extends _$ActivityNotifier2 {
/// Notifier arguments are specified on the build method.
/// There can be as many as you want, have any name, and even be optional/named.
@override
Future<Activity> build(String activityType) async {
// Arguments are also available with "this.<argumentName>"
print(this.activityType);

// TODO: perform a network request to fetch an activity
return fetchActivity();
}
}

0 comments on commit 0487d76

Please sign in to comment.