flutter开发HarmonyOS 鸿蒙Next flutter无法正常使用flutter_bloc

发布于 1周前 作者 eggper 最后一次编辑是 5天前 来自 Flutter

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

针对HarmonyOS 鸿蒙Next无法正常使用flutter_bloc的问题,以下是一些可能的解决方案:

  1. 环境检查:确保你的开发环境已正确搭建,包括安装鸿蒙开发工具DevEco Studio以及适配鸿蒙的Flutter SDK。Flutter SDK需选择dev版本,并确保其已更新到支持鸿蒙系统适配的版本。
  2. 依赖检查:检查pubspec.yaml文件中是否已正确添加了对flutter_bloc及其相关依赖的声明,并运行flutter pub get来更新依赖。
  3. 代码实现:确保flutter_bloc的使用方式正确,包括Event、State和Bloc类的定义,以及在UI中的正确使用。
  4. 插件支持:查看flutter_bloc的官方文档或GitHub仓库,确认其是否支持鸿蒙系统。如果不支持,可能需要寻找其他替代的状态管理库。
  5. 系统更新:鸿蒙Next系统可能还在不断完善中,建议检查是否有新的系统版本或Flutter SDK版本可用,并尝试更新。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部