return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>flutter - Is There a Way to Access a Provider from Within an Enum? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Is There a Way to Access a Provider from Within an Enum? - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论