flutter开发HarmonyOS 鸿蒙Next flutter无法正常使用flutter_bloc
HarmonyOS 鸿蒙Next flutter无法正常使用flutter_bloc
demo点击按钮报错
Error: Could not find the correct Provider<CounterBloc> above this CounterPage Widget
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class CounterEvent {}
class Increment extends CounterEvent {}
class Decrement extends CounterEvent {}
class CounterState {
final int count;
CounterState(this.count);
}
class InitialCounterState extends CounterState {
InitialCounterState() : super(0);
}
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(InitialCounterState()) {
on<Increment>((event, emit) => emit(CounterState(state.count + 1)));
on<Decrement>((event, emit) => emit(CounterState(state.count - 1)));
}
}
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<CounterBloc>(
create: (context) => CounterBloc(),
child: Scaffold(
appBar: AppBar(title: Text('Counter')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () =>
context.read<CounterBloc>().add(Increment()),
child: Text('Increment'),
),
ElevatedButton(
onPressed: () =>
context.read<CounterBloc>().add(Decrement()),
child: Text('Decrement'),
),
BlocBuilder<CounterBloc, CounterState>(
builder: (context, state) => Text(state.count.toString()),
),
],
),
),
),
);
}
}
2 回复
您可以参考以下代码:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
void main() {
runApp(MyApp());
}
abstract class CounterEvent {}
class CounterIncrementPressed extends CounterEvent {}
class CounterDecrementPressed extends CounterEvent {}
class CounterState {
final int counterValue;
CounterState({required this.counterValue});
}
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(CounterState(counterValue: 0)) {
on<CounterIncrementPressed>((event, emit) {
emit(CounterState(counterValue: state.counterValue + 1));
});
on<CounterDecrementPressed>((event, emit) {
emit(CounterState(counterValue: state.counterValue - 1));
});
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocProvider(
create: (context) => CounterBloc(),
child: CounterPage(),
),
);
}
}
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Counter')),
body: Center(
child: BlocBuilder<CounterBloc, CounterState>(
builder: (context, state) {
return Text(
'${state.counterValue}',
style: Theme.of(context).textTheme.headline4,
);
},
),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: () =>
context.read<CounterBloc>().add(CounterIncrementPressed()),
tooltip: 'Increment',
child: Icon(Icons.add),
),
SizedBox(height: 10),
FloatingActionButton(
onPressed: () =>
context.read<CounterBloc>().add(CounterDecrementPressed()),
tooltip: 'Decrement',
child: Icon(Icons.remove),
),
],
),
);
}
}