因此,我有一个项目处于混乱状态,我试图构建一个Cards列表,其内容取决于我的OrderModel类,并且我试图使用Provider来实现此目的,但是却出现此错误:>
════════调度程序库捕获了异常══════════════════════════
试图从小部件树外部监听提供者公开的值。
这可能是由于事件处理程序(如按钮的onPressed)调用了
没有通过listen: false
的Provider.of。
要修复,请写: Provider.of(context,listen:false);
它不受支持,因为可能无意义地重建与 事件处理程序,当小部件树不关心该值时。 'package:provider / src / provider.dart': 断言失败:第193行pos 7行:'context.owner.debugBuilding ||听==假|| _debugIsInInheritedProviderUpdate'
引发异常时,这是堆栈
#2 Provider.of 包:provider / src / provider.dart:193
#3 _OrderHistoryState._onAfterBuild 包:shinier_store / screens / order_history.dart:60
#4 _OrderHistoryState.build。 包:shinier_store / screens / order_history.dart:67
#5 SchedulerBinding._invokeFrameCallback 包:flutter /.../ scheduler / binding.dart:1102
#6 SchedulerBinding.handleDrawFrame 包:flutter /…/ scheduler / binding.dart:1049 ...
═══════════════════════════════════════════════ ══════════ 提供程序调用_onAfterBuild()的Stacktrace
I/flutter ( 3092): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
I/flutter ( 3092): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
I/flutter ( 3092): #2 Provider.of package:provider/src/provider.dart:193
I/flutter ( 3092): #3 _OrderHistoryState._onAfterBuild package:shinier_store/screens/order_history.dart:61
I/flutter ( 3092): #4 _OrderHistoryState.build.<anonymous closure>
package:shinier_store/screens/order_history.dart:71
I/flutter ( 3092): #5 SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1102
I/flutter ( 3092): #6 SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1049
I/flutter ( 3092): #7 SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:957
I/flutter ( 3092): #8 _rootRun (dart:async/zone.dart:1126:13)
I/flutter ( 3092): #9 _CustomZone.run (dart:async/zone.dart:1023:19)
I/flutter ( 3092): #10 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
I/flutter ( 3092): #11 _invoke (dart:ui/hooks.dart:259:10)
I/flutter ( 3092): #12 _drawFrame (dart:ui/hooks.dart:217:3)
我不知道如何解决该问题,因为我在Provider调用中添加了listen:false。我尝试使用WidgetBinding,因为我认为应该在构建完成后进行Provider调用,但这似乎无法解决问题。
以下是代码:
OrderModel类
class OrderModel extends ChangeNotifier {
List<Order> myOrders;
bool isLoading = true;
String errMsg;
int page = 1;
bool endPage = false;
void getMyOrder({UserModel userModel}) async {
try {
isLoading = true;
notifyListeners();
myOrders = await WooCommerce().getMyOrders(userModel: userModel, page: 1);
page = 1;
errMsg = null;
isLoading = false;
endPage = false;
notifyListeners();
} catch (err) {
errMsg =
"There is an issue with the app during request the data, please contact admin for fixing the issues " +
err.toString();
isLoading = false;
notifyListeners();
}
}
void loadMore({UserModel userModel}) async {
try {
isLoading = true;
page = page + 1;
notifyListeners();
var orders =
await WooCommerce().getMyOrders(userModel: userModel, page: page);
myOrders = [...myOrders, ...orders];
if (orders.length == 0) endPage = true;
errMsg = null;
isLoading = false;
notifyListeners();
} catch (err) {
errMsg =
"There is an issue with the app during request the data, please contact admin for fixing the issues " +
err.toString();
isLoading = false;
notifyListeners();
}
}
}
order_history.dart-状态类
class _OrderHistoryState extends State<OrderHistory> {
void _onAfterBuild(BuildContext context){
Provider.of<OrderModel>(context, listen: false)
.getMyOrder(userModel: Provider.of<UserModel>(context));
}
@override
Widget build(BuildContext context) {
var formatter = DateFormat('dd-MM-yyyy');
var model = Provider.of<OrderModel>(context);
WidgetsBinding.instance.addPostFrameCallback((_) => _onAfterBuild(context));
return Scaffold(
appBar: AppBar(
title: Text(
'Order History',
style: TextStyle(fontWeight: FontWeight.bold),
),
elevation: 0.0),
body: model.myOrders == null ? Center() :
Padding(
padding: const EdgeInsets.all(10.0),
child: ListView.separated(
separatorBuilder: (_, __) => SizedBox(height: 10.0),
itemCount: model.myOrders.length,
itemBuilder: (context, index) {
String stat = model.myOrders[index].status;
return Card(
color: _buildColor(stat),
elevation: 3.5,
...
}
}
main.dart-构建方法
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => UserModel()),
ChangeNotifierProvider(create: (_) => CartModel()),
ChangeNotifierProvider(create: (_) => SearchModel()),
ChangeNotifierProvider(create: (_) => OrderModel()),
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.white,
backgroundColor: Colors.white,
canvasColor: Colors.white,
),
home: MainTabs(),
debugShowCheckedModeBanner: false,
localizationsDelegates: [
i18n,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: i18n.supportedLocales,
),
);
}
答案 0 :(得分:2)
错误似乎指出这两行代码导致了问题。
#3 _OrderHistoryState._onAfterBuild package:shinier_store/screens/order_history.dart:60
#4 _OrderHistoryState.build. package:shinier_store/screens/order_history.dart:67
您提供的重现不完整,我只能猜测所需的标志 listen: false
已添加到 _onAfterBuild()
。但是,错误日志指出 var model = Provider.of<OrderModel>(context);
内的 Widget build()
也需要具有标志。 docs 中解释了此标志要求的原因。
如果您仍有问题,working minimal repro 将有助于我们了解为什么会出现这种行为。