我正在像这样使用库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":"错误状态:
试图读取在其创建过程中抛出的提供程序
值。\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
元素.reassemble
(package: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
元素.reassemble
(package: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
元素.reassemble
(package: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
元素.reassemble
(package: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
元素.reassemble
(package:flutter/src/widgets/framework.dart:3071:5)\n#17
元素.重新组装。
(包:flutter/src/widgets/framework.dart:3072:13)\n#18
ComponentElement.visitChildren
(package:flutter/src/widgets/framework.dart:4549:14)\n#19
元素.reassemble
(package: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
(package:flutter/src/widgets/binding.dart:1139:14)\n#23
元素.reassemble
(包:flutter/src/widgets/framework.dart:3071:5)\n#24
BuildOwner.reassemble
(包:flutter/src/widgets/framework.dart:2892:12)\n#25
WidgetsBinding.performReassemble
(package:flutter/src/widgets/binding.dart:951:19)\n#26
BindingBase.lockEvents
(package:flutter/src/foundation/binding.dart:298:41)\n#27
BindingBase.reassembleApplication
(package:flutter/src/foundation/binding.dart:338:12)\n#28
BindingBase.registerSignalServiceExtension。
(package:flutter/src/foundation/binding.dart:375:23)\n#29
BindingBase.registerSignalServiceExtension。
(package: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 类型的创建。
答案 0 :(得分:1)
注释掉这个
final MyRents myRents = MyRents();
并使用这个
ChangeNotifierProvider<MyRents>(create: (_) => MyRents())