flutter 提供程序:“错误状态:尝试读取在创建其值期间引发的提供程序”

zsohkypk  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(102)

我使用的库**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();

这在Flutter1中运行良好,但现在在Flutter2中出现此错误:
重新组装main. dart $main-2339422228391703失败:ext.flutter.reassemble:(-32000)服务器错误{"异常":"错误状态:试图读取在创建其值期间引发的提供程序。\n在创建类型MyRents期间发生异常。"," stack ":" #0_CreateInheritedProviderState. value(package:provider/src/inherited_provider. dart:661:7)\n#1
_继承提供程序作用域元素。重新汇编(包:提供程序/源/继承的提供程序。dart:367:60)\n#2
元素.重新组装.(包:flutter/src/widget/framework. dart:3072:13)\n#3
组件元素. visitChildren(软件包:flutter/src/widget/framework. dart:4549:14)\n#4
元素.重新组装(包:flutter/src/widget/framework. dart:3071:5)\n#5
元素.重新组装.(包:flutter/src/widget/framework. dart:3072:13)\n#6
组件元素. visitChildren(软件包:flutter/src/widget/framework. dart:4549:14)\n#7
元素.重新组装(包:flutter/src/widget/framework. dart:3071:5)\n#8
元素.重新组装.(包:flutter/src/widget/framework. dart:3072:13)\n#9
组件元素. visitChildren(软件包:flutter/src/widget/framework. dart:4549:14)\n#10
元素.重新组装(包:flutter/src/widget/framework. dart:3071:5)\n#11
元素.重新组装.(包:flutter/src/widget/framework. dart:3072:13)\n#12
组件元素. visitChildren(软件包:flutter/src/widget/framework. dart:4549:14)\n#13
元素.重新组装(包:flutter/src/widget/framework. dart:3071:5)\n#14
元素.重新组装.(包:flutter/src/widget/framework. dart:3072:13)\n#15
组件元素. visitChildren(软件包:flutter/src/widget/framework. dart:4549:14)\n#16
元素.重新组装(包:flutter/src/widget/framework. dart:3071:5)\n#17
元素.重新组装.(包:flutter/src/widget/framework. dart:3072:13)\n#18
组件元素. visitChildren(软件包:flutter/src/widget/framework. dart:4549:14)\n#19
元素。重新组装(包:flutter/src/widget/framework. dart:3071:5)\n#20
状态元素。重新汇编(软件包:flutter/src/widget/framework. dart:4624:11)\n#21
元素.重新组合.(软件包:flutter/src/widget/framework. dart:3072:13)\n#22
呈现对象到部件元素. visitChildren(包:flutter/src/部件/绑定. dart:1139:14)\n#23
元素.重新组装(软件包:flutter/src/widget/framework. dart:3071:5)\n#24
构建所有者。重新汇编(包:flutter/src/widget/framework. dart:2892:12)\n#25
小部件绑定.执行重新组装(软件包:flutter/src/widget/binding. dart:951:19)\n#26
绑定基础. lockEvents(数据包:flutter/src/foundation/绑定. dart:298:41)\n#27
绑定基础.重新组合应用程序(包:flutter/src/foundation/绑定. dart:338:12)\n#28
绑定基础.注册信号服务扩展.(软件包:flutter/src/foundation/binding. dart:375:23)\n#29
绑定基础.注册信号服务扩展.(软件包:flutter/src/foundation/binding. dart:374:17)\n#30
绑定基础.注册服务扩展.(软件包:flutter/src/foundation/binding. dart:597:32)\n\n ","方法:"扩展flutter.重新汇编"}
还有另一个
========= Flutter框架捕获到异常==========================================================================================================================================================================================================================================状态错误:尝试读取提供程序,但在创建其值期间引发了该提供程序.在创建类型MyRents期间发生异常.

jjjwad0x

jjjwad0x1#

将此注解掉
final MyRents myRents = MyRents();,并使用这个
ChangeNotifierProvider<MyRents>(create: (_) => MyRents())

相关问题