Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在命名路由器中传递参数的最佳方法_Flutter_Flutter Bloc - Fatal编程技术网

Flutter 在命名路由器中传递参数的最佳方法

Flutter 在命名路由器中传递参数的最佳方法,flutter,flutter-bloc,Flutter,Flutter Bloc,我有两个命名路由器小部件,我应该如何使用bloc模式将参数从一个传递到另一个 // main.dart void main() => runApp(App()); class App extends StatelessWidget { @override Widget build(BuildContext context) { return MultiBlocProvider( providers: [ BlocProvider<One

我有两个命名路由器小部件,我应该如何使用bloc模式将参数从一个传递到另一个

// main.dart

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<OneBloc>(create: (BuildContext context) => OneBloc()),
        BlocProvider<TwoBloc>(
            create: (BuildContext context) => TwoBloc())
      ],
      child: MaterialApp(
        title: 'testApp',
        initialRoute: '/one',
        routes: {
          '/one': (context) => One(),
          '/two': (context) => Two()
        },
      ),
    );
  }
}

// one_bloc.dart

class OneBloc extends Bloc<OneEvent, OneState> {
  OneBloc() : super(OneInitial());

  @override
  Stream<OneState> mapEventToState(
    OneEvent event,
  ) async* {
    if (event is PassParameter) {
      yield NavigateToTwo('parameter from One');
    }
  }
}

// one_state.dart

@immutable
abstract class OneState extends Equatable {
  const OneState();

  @override
  List<Object> get props => [];
}

class NavigateToTwo extends OneState {
  final String parameter;

  NavigateToTwo(this.parameter);
}

// One.dart -- part of UI code

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<OneBloc, OneState>(listener: (context, state) {
      if (state is NavigateToTwo) {
        Navigator.pushNamed(context, '/two'); // how to pass the state.parameter to TwoBloc
      }
    },
    // other code.
//main.dart
void main()=>runApp(App());
类应用程序扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回多BlocProvider(
供应商:[
BlocProvider(创建:(BuildContext上下文)=>OneBloc()),
BlocProvider(
create:(BuildContext context)=>TwoBloc()
],
孩子:MaterialApp(
标题:“testApp”,
initialRoute:“/one”,
路线:{
“/one”:(上下文)=>one(),
“/two”:(上下文)=>two()
},
),
);
}
}
//一团飞镖
类OneBloc扩展了Bloc{
OneBloc():super(OneInitial());
@凌驾
流映射事件状态(
OneEvent事件,
)异步*{
if(事件为PasspParameter){
产生两个(“一个参数中的参数”);
}
}
}
//一个州
@不变的
抽象类OneState扩展了equalable{
const one state();
@凌驾
列表获取道具=>[];
}
类NavigateToo扩展了一个状态{
最后一个字符串参数;
NavigateToTwo(this.parameter);
}
//One.dart--UI代码的一部分
@凌驾
小部件构建(构建上下文){
返回BlocConsumer(侦听器:(上下文,状态){
如果(状态为2){
Navigator.pushNamed(上下文“/two”);//如何将state.parameter传递给TwoBloc
}
},
//其他代码。
更新


在中检查todo示例后,它应该在TwoBloc中使用StreamSubscription来侦听OneBloc NavigateToo状态以获取参数。

首先,您需要创建一个类变量来接收要在类之间传递的参数

Class One {
  final a;
  One({this.a});
...
}
您可以像下面这样通过导航器传递参数

Map<String, dynamic> myArguments = {"argumentKey": value};
Navigator.pushNamed(context, '/two', arguments: myArgument);
MaterialApp(
  onGenerateRoute: (settings) {
  final args = settings.arguments;
  if (settings.name == '/one') {
    final argumentForPageOne = args["argumentForPageOne"];
  }
  if (settings.name == '/two') {
    final argumentForPageTwo = args["argumentForPageTwo"];
  }
  Map<String, Widget> widgets = {
    '/one': One(a: argumentForPageOne),
    '/two': Two(b: argumentForPageTwo)
 }
    return MaterialPageRoute(
      builder: (context) {
        return widgets[settings.name];
        },
      );
    }
  },
);
首先谈谈你的材料pp 加上这个

MaterialApp将在小部件树更改时运行onGenerateRoute。然后它具有(设置)属性,您可以使用这些属性获取参数

settings.arguments
如果你想知道这些该死的论点是从哪里来的,让我带你回到导航器

Usually, you can use it to pass the data on the arguments key, like this 
Navigator.pushNamed(context, "your routeName", arguments:"your object here");
让我们回到OnGeneratorOute 这是onGenerateRoute用法的完整示例

onGenerateRoute: (settings){
MaterialPageRoute routes;
switch (settings.name) {
    case OrderDetail.routeName:
      dynamic args = settings.arguments;
      routes = MaterialPageRoute(
        builder: (context) {
          return OrderDetail(args);
        },
      );
      break;
    case ShowLocation.routeName:
      dynamic args = settings.arguments;
      routes = MaterialPageRoute(
        builder: (context) {
          return ShowLocation(args);
        },
      );
      break;
    case AddressDetail.routeName:
      dynamic args = settings.arguments;
      routes = MaterialPageRoute(
        builder: (context) {
          return AddressDetail(args);
        },
      );
      break;
  }
  return routes;
}
结论:

onGenerateRoute: (settings){
    settings.name;/// This will return a routeName which you called on the navigator pushNamed
    settings.arguments;/// This will return an object which you invoked on the navigator pushNamed it can be anything (dynamic) but also it can be specific, depends on what you set on the onGenerate Route !!
    ///Navigator.pushNamed(context, routeName, arguments:"your args");
}
祝你好运

Usually, you can use it to pass the data on the arguments key, like this 
Navigator.pushNamed(context, "your routeName", arguments:"your object here");
onGenerateRoute: (settings){
MaterialPageRoute routes;
switch (settings.name) {
    case OrderDetail.routeName:
      dynamic args = settings.arguments;
      routes = MaterialPageRoute(
        builder: (context) {
          return OrderDetail(args);
        },
      );
      break;
    case ShowLocation.routeName:
      dynamic args = settings.arguments;
      routes = MaterialPageRoute(
        builder: (context) {
          return ShowLocation(args);
        },
      );
      break;
    case AddressDetail.routeName:
      dynamic args = settings.arguments;
      routes = MaterialPageRoute(
        builder: (context) {
          return AddressDetail(args);
        },
      );
      break;
  }
  return routes;
}
onGenerateRoute: (settings){
    settings.name;/// This will return a routeName which you called on the navigator pushNamed
    settings.arguments;/// This will return an object which you invoked on the navigator pushNamed it can be anything (dynamic) but also it can be specific, depends on what you set on the onGenerate Route !!
    ///Navigator.pushNamed(context, routeName, arguments:"your args");
}