【发布时间】:2021-11-13 22:01:57
【问题描述】:
我在两个类中使用提供程序,一个工作正常,另一个不工作。我在 main.dart 文件中初始化了提供程序,如下所示
P.S(PersonalPostingService 是一个 chopper 服务)但它在其他类中工作正常
Widget build(BuildContext context) {
return Provider(
create: (_) => PersonalPostingService.create(),
dispose: (context, PersonalPostingService service) =>
service.client.dispose(),
child: MaterialApp(
home: WeeklyData(),
));
}
使用提供程序调用函数时出现以下错误
E/flutter (16561): This is likely caused by an event handler (like a button's onPressed) that called
E/flutter (16561): Provider.of without passing `listen: false`.
E/flutter (16561):
E/flutter (16561): To fix, write:
E/flutter (16561): Provider.of<PersonalPostingService>(context, listen: false);
E/flutter (16561):
E/flutter (16561): It is unsupported because may pointlessly rebuild the widget associated to the
E/flutter (16561): event handler, when the widget tree doesn't care about the value.
E/flutter (16561):
E/flutter (16561): The context used was: FormClass
这是我调用提供者的地方(Statlesswidget 类)
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
// ignore: deprecated_member_use
child: RaisedButton(
child: Text("Send"),
onPressed: () async {
await Provider.of<PersonalPostingService>(context)
.postPersonal(data);
}),
),
);
}
如果我添加 Listen:false,它就会这样做
Launching lib\main.dart on vivo 1906 in debug mode...
lib\main.dart:1
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Error waiting for a debug connection: The log reader stopped unexpectedly
Error launching application on vivo 1906.
Exited (sigterm)
【问题讨论】:
标签: flutter dart flutter-provider