Flutter 如何将Flatter_bloc与方法通道集成?
我已经开始使用flatter\u bloc包而不是redux来尝试它,但我不完全确定从本机(Android/iOS)接收东西时如何调用flatter bloc事件。使用redux更容易,因为在我的main.dart文件的父MyApp小部件中,我将redux存储传递给我创建的自定义类,并从所述类(称为MethodChannelHandler)调度方法 main.dart:Flutter 如何将Flatter_bloc与方法通道集成?,flutter,bloc,flutter-bloc,flutter-method-channel,Flutter,Bloc,Flutter Bloc,Flutter Method Channel,我已经开始使用flatter\u bloc包而不是redux来尝试它,但我不完全确定从本机(Android/iOS)接收东西时如何调用flatter bloc事件。使用redux更容易,因为在我的main.dart文件的父MyApp小部件中,我将redux存储传递给我创建的自定义类,并从所述类(称为MethodChannelHandler)调度方法 main.dart: void main() { runApp(new MyApp()); } class MyApp extends St
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final Store<AppState> store = Store<AppState>(
// ... redux stuff ...
);
@override
void initState() {
// sauce
MethodChannelHandler(store);
super.initState();
}
}
void main(){
runApp(新的MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
State createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
最终存储=存储(
//…重复的东西。。。
);
@凌驾
void initState(){
//酱汁
方法渠道处理者(商店);
super.initState();
}
}
methodChannelHandler.dart:
class MethodChannelHandler {
Store<AppState> store;
MethodChannelHandler(this.store) {
methodChannel.setMethodCallHandler(_handleMethod);
}
// Handle method calls from native
Future _handleMethod(MethodCall call) async {
if (call.method == A_METHOD) {
store.dispatch("something from native")
}
}
}
类方法ChannelHandler{
商店;
MethodChannelHandler(this.store){
methodChannel.setMethodCallHandler(_handleMethod);
}
//处理来自本机的方法调用
Future\u handleMethod(MethodCall调用)异步{
if(call.method==A_方法){
store.dispatch(“来自本地的东西”)
}
}
}
注意:我不擅长编程词汇,所以如果可能的话,请给我一小段类似的示例代码,或者将我链接到一些我可以参考的GitHub repo而不是给我一段我可能不理解的文本。用非常简单的方式,它看起来是这样的:
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<SomeBloc>(
create: (_) {
final bloc = SomeBloc(); //Create bloc
MethodChannelHandler(bloc); //Add method handler
return bloc;
},
lazy: false,
child: Text("Content"),
);
}
}
class SomeBloc extends Bloc {
SomeBloc() : super(SomeInitState());
@override
Stream mapEventToState(event) async* {
if (event is SomeEvent) {
//Handle SomeEvent
}
}
}
class MethodChannelHandler {
final SomeBloc someBloc;
MethodChannelHandler(this.someBloc) {
methodChannel.setMethodCallHandler(_handleMethod);
}
// Handle method calls from native
Future _handleMethod(MethodCall call) async {
if (call.method == A_METHOD) {
someBloc.add(SomeEvent("something from native"));
}
}
}
类应用程序扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回BlocProvider(
创建:(){
final bloc=SomeBloc();//创建bloc
MethodChannelHandler(bloc);//添加方法处理程序
返回集团;
},
懒惰:错,
子项:文本(“内容”),
);
}
}
类SomeBloc扩展了Bloc{
SomeBloc():super(SomeInitState());
@凌驾
流mapEventToState(事件)异步*{
if(event是SomeEvent){
//处理某事
}
}
}
类MethodChannelHandler{
最后一个SomeBloc SomeBloc;
MethodChannelHandler(this.someBloc){
methodChannel.setMethodCallHandler(_handleMethod);
}
//处理来自本机的方法调用
Future\u handleMethod(MethodCall调用)异步{
if(call.method==A_方法){
添加(SomeEvent(“来自本地的东西”);
}
}
}
非常简单,它看起来是这样的:
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<SomeBloc>(
create: (_) {
final bloc = SomeBloc(); //Create bloc
MethodChannelHandler(bloc); //Add method handler
return bloc;
},
lazy: false,
child: Text("Content"),
);
}
}
class SomeBloc extends Bloc {
SomeBloc() : super(SomeInitState());
@override
Stream mapEventToState(event) async* {
if (event is SomeEvent) {
//Handle SomeEvent
}
}
}
class MethodChannelHandler {
final SomeBloc someBloc;
MethodChannelHandler(this.someBloc) {
methodChannel.setMethodCallHandler(_handleMethod);
}
// Handle method calls from native
Future _handleMethod(MethodCall call) async {
if (call.method == A_METHOD) {
someBloc.add(SomeEvent("something from native"));
}
}
}
类应用程序扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回BlocProvider(
创建:(){
final bloc=SomeBloc();//创建bloc
MethodChannelHandler(bloc);//添加方法处理程序
返回集团;
},
懒惰:错,
子项:文本(“内容”),
);
}
}
类SomeBloc扩展了Bloc{
SomeBloc():super(SomeInitState());
@凌驾
流mapEventToState(事件)异步*{
if(event是SomeEvent){
//处理某事
}
}
}
类MethodChannelHandler{
最后一个SomeBloc SomeBloc;
MethodChannelHandler(this.someBloc){
methodChannel.setMethodCallHandler(_handleMethod);
}
//处理来自本机的方法调用
Future\u handleMethod(MethodCall调用)异步{
if(call.method==A_方法){
添加(SomeEvent(“来自本地的东西”);
}
}
}
ooo请稍候,您是否有使用MultiBlocProvider的示例?因为MultiBlocProvider为每个bloc都有一个create。这是否意味着每个阵营我都需要多个MethodChannels?@JohnnyBoy你需要在每个阵营听香奈儿的留言?你想要听什么消息?我正在制作一个音乐应用程序,我想它至少需要两个群组(可能更多),每个群组发送不同类型的数据:从暂停/播放/回放歌曲的单个字符串到表示歌曲的对象集合/数组。我不熟悉BLoC,所以也许我只是没有正确地设计我的项目(我应该尝试为所有methodchannel交互创建一个单独的BLoC吗?)。我也在想,也许我可以嵌套需要methodchannels的BLoCs,并使用孩子的上下文来调用context.read。¯_(ツ)_/''我认为您可以在所有交互中使用单个bloc,但可以向bloc发送不同的事件。您可以将一个bloc传递给另一个bloc,并像bloc Todoes示例一样侦听其状态ooo等等,您有使用MultiBlocProvider的示例吗?因为MultiBlocProvider为每个bloc都有一个create。这是否意味着我需要多个bloc每个阵营有多少条消息?@JohnnyBoy你需要在每个阵营听香奈儿的消息?你想要听什么消息?我正在制作一个音乐应用程序,我想它至少需要两个阵营听(可能更多),每个都发送不同类型的数据:从用于暂停/播放/回放歌曲的单个字符串,到表示歌曲的对象集合/数组。我不熟悉BLoCs,所以可能我只是没有正确地设计我的项目(我应该尝试为所有methodchannel交互创建单个BLoC吗?)。我也在想,也许我可以嵌套需要MethodChannel的bloc,并使用子对象的上下文调用context.read。'_(ツ)_/''我认为您可以对所有交互使用单个bloc,但可以向bloc发送不同的事件。您可以将一个bloc传递给另一个bloc,并像bloc Todoes示例中那样侦听其状态