Flutter,一点 Cubit BlocBuilder 问题
Flutter, a little Cubit BlocBuilder problem
我正在尝试使用 BlockBuilder 显示我的 Cubit int 状态,这是我的代码:
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
void decrement() => emit(state - 1);
}
class CounterContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(create: (_) => CounterCubit(), child: CounterView());
}
}
void main() {
runApp(MaterialApp(
theme: myTheme,
home: CounterView(),
));
}
class CounterView extends StatelessWidget {
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text("Example"),
),
body: Center( //I guess the problem is here
child: BlocBuilder<CounterCubit, int>(builder: (context, state) {
return Text("$state", style: textTheme.headline2);
})),
//Increment and decrement button
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => context.read<CounterCubit>().increment(),
),
const SizedBox(
height: 8,
),
FloatingActionButton(
child: const Icon(Icons.remove),
onPressed: () => context.read<CounterCubit>().decrement(),
)
],
),
);
}
}
控制台错误是
Error: Could not find the correct Provider above this BlocBuilder<CounterCubit, int> Widget
我已经尝试使用 Provider 但它没有解决问题
您没有使用 CounterContainer
,因此您没有 CounterCubit。
应该是:
void main() {
runApp(MaterialApp(
theme: myTheme,
home: CounterContainer(),
));
}
我正在尝试使用 BlockBuilder 显示我的 Cubit int 状态,这是我的代码:
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
void decrement() => emit(state - 1);
}
class CounterContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(create: (_) => CounterCubit(), child: CounterView());
}
}
void main() {
runApp(MaterialApp(
theme: myTheme,
home: CounterView(),
));
}
class CounterView extends StatelessWidget {
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text("Example"),
),
body: Center( //I guess the problem is here
child: BlocBuilder<CounterCubit, int>(builder: (context, state) {
return Text("$state", style: textTheme.headline2);
})),
//Increment and decrement button
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => context.read<CounterCubit>().increment(),
),
const SizedBox(
height: 8,
),
FloatingActionButton(
child: const Icon(Icons.remove),
onPressed: () => context.read<CounterCubit>().decrement(),
)
],
),
);
}
}
控制台错误是
Error: Could not find the correct Provider above this BlocBuilder<CounterCubit, int> Widget
我已经尝试使用 Provider 但它没有解决问题
您没有使用 CounterContainer
,因此您没有 CounterCubit。
应该是:
void main() {
runApp(MaterialApp(
theme: myTheme,
home: CounterContainer(),
));
}