gpt4 book ai didi

flutter - 试图从小部件树外部监听提供者公开的值

转载 作者:行者123 更新时间:2023-12-03 13:29:18 25 4
gpt4 key购买 nike

所以,我有一个 flutter 项目,我正在尝试构建一个卡片列表,其中的内容取决于我的 OrderModel 类,我正在尝试使用 Provider 来实现这一点,但我收到了这个错误:

========调度程序库捕获的异常==========================

试图从小部件树外部监听提供者公开的值。

这很可能是由调用的事件处理程序(如按钮的 onPressed)引起的
Provider.of 未通过 listen: false .

要修复,请编写:
Provider.of(context, 监听: false);

它不受支持,因为可能会毫无意义地重建与
事件处理程序,当小部件树不关心值时。
'包:提供者/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() 中提供程序调用的堆栈跟踪

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 调用应该在构建完成后进行,但这似乎并没有解决问题。

以下是代码:

订单模型类

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,
),
);
}

最佳答案

该错误似乎指出这两行代码导致了问题。

#3 _OrderHistoryState._onAfterBuild package:shinier_store/screens/order_history.dart:60

#4 _OrderHistoryState.build. package:shinier_store/screens/order_history.dart:67
您提供的 repro 不完整,我只能猜测所需的标志 listen: false已添加到 _onAfterBuild() .但是,错误日志指出 var model = Provider.of<OrderModel>(context);Widget build()还需要有flag。此标志要求的原因在 docs 中进行了解释。 .
如果您仍有问题,请发送 working minimal repro将有助于我们了解为什么会发生这种行为。

关于flutter - 试图从小部件树外部监听提供者公开的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60083895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com