提供者:“错误状态:试图读取在创建其价值期间抛出的提供者”

Posted

技术标签:

【中文标题】提供者:“错误状态:试图读取在创建其价值期间抛出的提供者”【英文标题】:Provider: "Bad state: Tried to read a provider that threw during the creation of its value" 【发布时间】:2021-06-25 18:31:39 【问题描述】:

我正在像这样使用库 Provider

runApp(
    LocalizedApp(
      delegate,
      MyApp(
        localizedValues: ,
      ),
    ),
  );

然后:

class MyApp extends StatelessWidget 
  final Map<String, Map<String, String>> localizedValues;
  MyApp(required this.localizedValues);

  @override
  Widget build(BuildContext context) 
    final localizationDelegate = LocalizedApp.of(context).delegate;

    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => MyRents()),
      ],
      child: MaterialApp(
        initialRoute: initialRoute,
    localizationsDelegates: [
      GlobalMaterialLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      localizationDelegate
    ],

这是我的提供者:

class MyRents extends ChangeNotifier 
// LIST OF VARS NOT USED AT THIS POINT

  bool loadingUserInfo = false;
  var language;
  // List list = rentsList;
  List list = [];
  int currentPortfolioIndex = 0;
  int currenRentIndex = 0;
  var clonedRent;
  List newGalleryImages = []; // local gallery images that we haven't pushed yet
  SharedPreferences? prefs;
  String viewMode = 'multi';
  bool galleryScrollView = true;
  PageController portfolioController = PageController(initialPage: 0);
  MyUser? user; // TODO: move this to a separate provider
  List<ProductDetails> products = [];
  List<PurchaseDetails> purchases = [];
  bool showAds = false;
  Map companyInfo = ;
  List withholdings = [];
  List<QueryDocumentSnapshot> taxes = [];
// END LIST


  // MyRents(); // TODO: check null (this was working in Flutter 1 but not now so it might be related to the error)...
  final MyRents myRents = MyRents(); // ...and this is the Flutter 2 version

  updateUI() 
    notifyListeners();
  


final MyRents myRents = MyRents();

这在 Flutter 1 中运行良好,但我现在在 Flutter 2 中遇到此错误:

重新组装 main.dart$main-2339422228391703 失败: ext.flutter.reassemble: (-32000) 服务器错误 "exception":"Bad state: 试图读取在其创建过程中抛出的提供程序 值。\n创建类型时发生异常 MyRents.","stack":"#0 _CreateInheritedProviderState.value (包:provider/src/inherited_provider.dart:661:7)\n#1 _InheritedProviderScopeElement.reassemble (package:provider/src/inherited_provider.dart:367:60)\n#2 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#3 ComponentElement.visitChildren (包:flutter/src/widgets/framework.dart:4549:14)\n#4 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#5 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#6 ComponentElement.visitChildren (包:flutter/src/widgets/framework.dart:4549:14)\n#7 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#8 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#9 ComponentElement.visitChildren (包:flutter/src/widgets/framework.dart:4549:14)\n#10 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#11 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#12 ComponentElement.visitChildren (包:flutter/src/widgets/framework.dart:4549:14)\n#13 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#14 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#15 ComponentElement.visitChildren (包:flutter/src/widgets/framework.dart:4549:14)\n#16 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#17 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#18 ComponentElement.visitChildren (包:flutter/src/widgets/framework.dart:4549:14)\n#19 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#20 StatefulElement.reassemble (包:flutter/src/widgets/framework.dart:4624:11)\n#21 元素。重新组装。 (包:flutter/src/widgets/framework.dart:3072:13)\n#22 RenderObjectToWidgetElement.visitChildren (包:flutter/src/widgets/binding.dart:1139:14)\n#23 元素重组 (包:flutter/src/widgets/framework.dart:3071:5)\n#24 BuildOwner.reassemble (包:flutter/src/widgets/framework.dart:2892:12)\n#25 WidgetsBinding.performReassemble (包:flutter/src/widgets/binding.dart:951:19)\n#26 BindingBase.lockEvents (包:flutter/src/foundation/binding.dart:298:41)\n#27 BindingBase.reassembleApplication (包:flutter/src/foundation/binding.dart:338:12)\n#28 BindingBase.registerSignalServiceExtension。 (包:flutter/src/foundation/binding.dart:375:23)\n#29 BindingBase.registerSignalServiceExtension。 (包:flutter/src/foundation/binding.dart:374:17)\n#30 BindingBase.registerServiceExtension。 (package:flutter/src/foundation/binding.dart:597:32)\n\n","method":"ext.flutter.reassemble"

还有这个:

======== Flutter 框架捕获的异常 ================================== =================== 在服务扩展回调期间抛出以下 StateError “ext.flutter.reassemble”:错误状态:试图读取提供程序 在创造其价值的过程中抛出。异常发生在 MyRents 类型的创建。

【问题讨论】:

请发布MyRents 提供商。错误是在构建它的过程中的宝座。 完成。我删除了一些不必要的代码。那时什么都没有被调用(我猜) “试图读取在创建其值期间抛出的提供程序。”错误出现在那里。如果该类的变量为空,则可以在 build 或 didChangeDependencies 上使用 final 变量。 评论这个final MyRents myRents = MyRents();,并使用这个ChangeNotifierProvider&lt;MyRents&gt;(create: (_) =&gt; MyRents()),你在哪里调用你的价值观来倾听? 显示你调用的值。 【参考方案1】:

评论一下

final MyRents myRents = MyRents(); 也使用这个

ChangeNotifierProvider&lt;MyRents&gt;(create: (_) =&gt; MyRents())

【讨论】:

以上是关于提供者:“错误状态:试图读取在创建其价值期间抛出的提供者”的主要内容,如果未能解决你的问题,请参考以下文章

DUBBO 直连提供者方法

Consul 实现服务提供者和服务消费者

没有 ControlContainer 提供者,也没有 ControlContainer 提供者

实体框架:提供者未返回提供者清单实例

Laravel服务/服务提供者/容器/契约和门面

服务提供者框架模式