🍑

familyとrecordは一緒に使えるらしい?

2023/10/30に公開

Overview

riverpodのfamilyとDart3のrecordは組み合わせることができるらしい?
Records
https://dart.dev/language/records
K9iさんの記事
https://zenn.dev/k9i/articles/754f097b86911e#family

簡単なものしか思いつかなかったのでとりあえず作ってみた。

summary

riverpodのfamilyは、パラメーターを渡すだけのようだ。。。
多分そうだと思う。とりあえずやってみよう!

import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'fruit.g.dart';

// 果物の種類をenumで定義
enum Fruit {
  apple,
  banana,
  orange,
}

// generatorを使わない場合
// final fruitProvider = Provider.family<String, Fruit>((ref, fruit) {
//   var record = ('🍎', '🍌', '🍊');
//   return switch (fruit) {
//     Fruit.apple => record.$1,
//     Fruit.banana => record.$2,
//     Fruit.orange => record.$3,
//   };
// });

// generatorを使う場合は、ref, の後に引数を渡すと、familyになるらしい?

String fruits(FruitsRef ref, Fruit fruit) {
  // Dart3のrecordを使って、複数の値を返す。
  var record = ('🍎', '🍌', '🍊');
  // switchで、enumの値によって、返す値を変える。名前付きではないので、$1, $2, $3となる。
  return switch (ref.fruit) {
    Fruit.apple => record.$1,
    Fruit.banana => record.$2,
    Fruit.orange => record.$3,
  };
}

自動生成するコマンドを実行する

flutter pub run build_runner watch --delete-conflicting-outputs

🤖こちらが自動生成されたコード

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'fruit.dart';

// **************************************************************************
// RiverpodGenerator
// **************************************************************************

String _$fruitsHash() => r'516a4a384f0de87f6871349b09ef783284b88dff';

/// Copied from Dart SDK
class _SystemHash {
  _SystemHash._();

  static int combine(int hash, int value) {
    // ignore: parameter_assignments
    hash = 0x1fffffff & (hash + value);
    // ignore: parameter_assignments
    hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
    return hash ^ (hash >> 6);
  }

  static int finish(int hash) {
    // ignore: parameter_assignments
    hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
    // ignore: parameter_assignments
    hash = hash ^ (hash >> 11);
    return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
  }
}

/// See also [fruits].
(fruits)
const fruitsProvider = FruitsFamily();

/// See also [fruits].
class FruitsFamily extends Family<String> {
  /// See also [fruits].
  const FruitsFamily();

  /// See also [fruits].
  FruitsProvider call(
    Fruit fruit,
  ) {
    return FruitsProvider(
      fruit,
    );
  }

  
  FruitsProvider getProviderOverride(
    covariant FruitsProvider provider,
  ) {
    return call(
      provider.fruit,
    );
  }

  static const Iterable<ProviderOrFamily>? _dependencies = null;

  
  Iterable<ProviderOrFamily>? get dependencies => _dependencies;

  static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;

  
  Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
      _allTransitiveDependencies;

  
  String? get name => r'fruitsProvider';
}

/// See also [fruits].
class FruitsProvider extends AutoDisposeProvider<String> {
  /// See also [fruits].
  FruitsProvider(
    Fruit fruit,
  ) : this._internal(
          (ref) => fruits(
            ref as FruitsRef,
            fruit,
          ),
          from: fruitsProvider,
          name: r'fruitsProvider',
          debugGetCreateSourceHash:
              const bool.fromEnvironment('dart.vm.product')
                  ? null
                  : _$fruitsHash,
          dependencies: FruitsFamily._dependencies,
          allTransitiveDependencies: FruitsFamily._allTransitiveDependencies,
          fruit: fruit,
        );

  FruitsProvider._internal(
    super._createNotifier, {
    required super.name,
    required super.dependencies,
    required super.allTransitiveDependencies,
    required super.debugGetCreateSourceHash,
    required super.from,
    required this.fruit,
  }) : super.internal();

  final Fruit fruit;

  
  Override overrideWith(
    String Function(FruitsRef provider) create,
  ) {
    return ProviderOverride(
      origin: this,
      override: FruitsProvider._internal(
        (ref) => create(ref as FruitsRef),
        from: from,
        name: null,
        dependencies: null,
        allTransitiveDependencies: null,
        debugGetCreateSourceHash: null,
        fruit: fruit,
      ),
    );
  }

  
  AutoDisposeProviderElement<String> createElement() {
    return _FruitsProviderElement(this);
  }

  
  bool operator ==(Object other) {
    return other is FruitsProvider && other.fruit == fruit;
  }

  
  int get hashCode {
    var hash = _SystemHash.combine(0, runtimeType.hashCode);
    hash = _SystemHash.combine(hash, fruit.hashCode);

    return _SystemHash.finish(hash);
  }
}

mixin FruitsRef on AutoDisposeProviderRef<String> {
  /// The parameter `fruit` of this provider.
  Fruit get fruit;
}

class _FruitsProviderElement extends AutoDisposeProviderElement<String>
    with FruitsRef {
  _FruitsProviderElement(super.provider);

  
  Fruit get fruit => (origin as FruitsProvider).fruit;
}
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member

文法の勉強ばかりしてもつまらないから、Widgetで使うにはどうすればいいのか実験してみた!

import 'package:dart_three_example/example/record/provider/fruit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class FruitKind extends ConsumerWidget {
  const FruitKind({super.key});

  
  Widget build(BuildContext context, WidgetRef ref) {
    // 変数を1個ずつ定義して、値を使う。
    final apple = ref.watch(fruitsProvider(Fruit.apple));
    final banana = ref.watch(fruitsProvider(Fruit.banana));
    final orange = ref.watch(fruitsProvider(Fruit.orange));
    return Scaffold(
      appBar: AppBar(
        title: const Text('Fruit Kind'),
      ),
      body: Center(
        child: Column(
          children: [
            // 後は、使うだけ。
            Text(apple),
            Text(banana),
            Text(orange),
          ],
        ),
      ),
    );
  }
}

main.dartでimportしてビルドしてみてください。

main.dart
import 'package:dart_three_example/example/record/ui/fruit_kind.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const FruitKind(),
    );
  }
}

なぜか色がついてない???、まあいいか。

thoughts

すごく単純だけど、どこにも情報がなかったので、思いつきで作ってみた。誰かのお役に立てそうかなと思って記事を書いてみました。

Discussion