英文:
Error: "The expression doesn't evaluate to a function, so it can't be invoked." when using Riverpod's watch in Flutter
问题
我正在尝试将我的Flutter项目中的代码从GetX迁移到Riverpod。但是,当我在Riverpod的Consumer中使用watch时,遇到了一个错误,错误消息是:“该表达式不会评估为函数,因此无法调用”。
以下是错误发生的代码片段:
child: Consumer(
builder: (ref, watch, child) {
final startPausedText =
watch(countDownControllerProvider).state.isAnimating;
// 其余的代码...
},
),
错误具体指向这一行:final startPausedText = watch(countDownControllerProvider).state.isAnimating
。我不确定是什么原因导致了这个问题。
这是完整的代码,为问题提供了上下文:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pomoworkojunio2023/home_screen/5.pomodoro_intervals/5.pomodoro_intervals.dart';
final countDownControllerProvider =
StateProvider<CountDownController>((ref) => CountDownController());
class StartStopGroupButton extends ConsumerStatefulWidget {
const StartStopGroupButton({Key? key}) : super(key: key);
@override
ConsumerState<StartStopGroupButton> createState() =>
_StartStopGroupButtonState();
}
class _StartStopGroupButtonState extends ConsumerState<StartStopGroupButton> {
late final countDownController;
@override
void initState() {
super.initState();
countDownController = ref.read(countDownControllerProvider);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Center(
child: Consumer(
builder: (ref, watch, child) {
final startPausedText =
watch(countDownControllerProvider).state.isAnimating;
return FloatingActionButton.extended(
heroTag: 'btn1',
elevation: 0,
backgroundColor: Colors.white,
onPressed: countDownController.startPaused,
label: Text(
startPausedText,
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
),
),
);
},
),
),
);
}
}
请问是否有人可以帮助我理解为什么会出现这个错误,以及如何解决它?我试图使用Riverpod的watch来访问countDownControllerProvider
的状态,并相应地更新UI。非常感谢您提供的任何帮助或指导。
英文:
I'm trying to migrate my code from GetX to Riverpod in my Flutter project. However, I encountered an error that says "The expression doesn't evaluate to a function, so it can't be invoked" when using watch with Riverpod's Consumer.
Here's the code snippet where the error occurs:
child: Consumer(
builder: (ref, watch, child) {
final startPausedText =
watch(countDownControllerProvider).state.isAnimating;
// Rest of the code...
},
),
The error specifically points to the line: final startPausedText = watch(countDownControllerProvider).state.isAnimating
;. I'm not sure what could be causing this issue.
This is the complete code, which provides context for the problem:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pomoworkojunio2023/home_screen/5.pomodoro_intervals/5.pomodoro_intervals.dart';
final countDownControllerProvider =
StateProvider<CountDownController>((ref) => CountDownController());
class StartStopGroupButton extends ConsumerStatefulWidget {
const StartStopGroupButton({Key? key}) : super(key: key);
@override
ConsumerState<StartStopGroupButton> createState() =>
_StartStopGroupButtonState();
}
class _StartStopGroupButtonState extends ConsumerState<StartStopGroupButton> {
late final countDownController;
@override
void initState() {
super.initState();
countDownController = ref.read(countDownControllerProvider);
}
@override
Widget build(BuildContext context, ) {
return Scaffold(
backgroundColor: Colors.grey,
body: Center(
child: Consumer(
builder: (ref, watch, child) {
final startPausedText =
watch(countDownControllerProvider).state.isAnimating;
return FloatingActionButton.extended(
heroTag: 'btn1',
elevation: 0,
backgroundColor: Colors.white,
onPressed: countDownController.startPaused,
label: Text(
startPausedText,
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
),
),
);
},
),
),
);
}
}
Could someone please help me understand why I'm getting this error and how I can resolve it? I'm trying to use Riverpod's watch to access the state from the countDownControllerProvider
and update the UI accordingly.
Thank you in advance for any assistance or guidance you can provide.
答案1
得分: 1
builder: (ref, watch, child)
不是 Consumer
的正确参数。你需要使用 (context, ref, child)
。然后,你可以调用 ref.watch
来完成你想要用 watch
实现的功能。
英文:
builder: (ref, watch, child)
is not the proper parameters for a Consumer
. You want (context, ref, child)
. And then you'll call ref.watch
to do what you're trying to do with just watch
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论