I have several pages in the Flutter app I'm working on that are all structurally identical. The only difference between them is the type of data they display.
I've been playing around with enhanced enums in Dart/Flutter, and I'm wondering if there's a way to reference a Riverpod provider from within the enum? I've tried this...
import 'package:flipbooks/screens/current/costcode_flow/costcode_repo.dart';
import 'package:flipbooks/screens/current/job_flow/job_repo.dart';
import 'package:flipbooks/screens/current/payee_flow/payee_repo.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
enum Elements {
job(
type: "Job",
elementListProvider: jobsListFutureProvider, // error
txnsListProvider: "transactionsListForJobProvider",
),
payee(
type: "Payee",
elementListProvider: payeesListFutureProvider, // error
txnsListProvider: "path/to/provider",
),
costcode(
type: "Costcode",
elementListProvider: costcodesListFutureProvider, // error
txnsListProvider: "transactionsListForCostcodeProvider",
);
const Elements(
{required this.type,
required this.elementListProvider,
required this.txnsListProvider});
final String type;
final ProviderListenable elementListProvider;
final String txnsListProvider;
}
...but it doesn't work. When I try to access the provider field using final listValue = ref.watch(test.elementListProvider);
I get an error that a String can't be assigned to the parameter type 'ProviderListenable'. I can change the type within the enum from String
to ProviderListenable
but then the reference (name of the provider) generates the same error (i.e. can't assign a String...).
Just wondering if there's a way to do this, or if I just need to use a switch/case or similar structure instead.