【问题标题】:How to pass data in flutter on Stateless Widget?如何在无状态小部件上颤振传递数据?
【发布时间】:2022-02-10 09:27:46
【问题描述】:

我正在尝试完成我的学校作业,但在将数据传递到无状态小部件时遇到了困难。我有以下课程:

    class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Page1(),
    );
  }
}




class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: ElevatedButton(
        onPressed: () {
          Navigator.push(
              context, MaterialPageRoute(builder: (context) => Page2(messageData: messageData)));
        },
        child: Text('Request Chat'),
      )),
    );
  }
}




class Page2 extends StatelessWidget {
  //Navigation
  static Route route(MessageData data) => MaterialPageRoute(
        builder: (context) => Page2(
          messageData: data,
        ),
      );

  const Page2({Key? key, required this.messageData}) : super(key: key);

  final MessageData messageData;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: Theme.of(context).iconTheme,
        centerTitle: false,
),
),
},


@immutable
class MessageData {
  const MessageData(
      {required this.senderName,
      required this.message,
      required this.messageDate,
      required this.dateMessage,
      required this.profilePicture});

  final String senderName;
  final String message;
  final DateTime messageDate;
  final String dateMessage;
  final String profilePicture;
}




上面有我的messageData需要传递。

基本上,我希望能够导航到第 2 页,但在 Navigator.push() 处不断收到错误消息。 错误详情:Type' can't be assigned to the parameter type 'MessageData'.

当 Page2() 上没有参数时,错误消息:The named parameter 'messageData' is required, but there's no corresponding argument.

【问题讨论】:

    标签: flutter navigation


    【解决方案1】:

    您必须使用构造函数传递数据。我希望你能从这个link得到一个想法。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    您可以像传递给Flutter 的小部件一样将参数传递给小部件:

    ///Creating Page2 instances
    Page2(text: 'text');
    Page2.otherConstructor('text');
    
    ///This is how you pass values to widgets
    class Page2 extends StatelessWidget {
      //Pass `this.field` to the constructor so that you are asking for the field you created.
      const Page2({Key? key, required this.text}) : super(key: key);
      const Page2.otherConstructor(this.text);
    
      final String text;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Text(text),
        );
      }
    }
    
    

    如果它是State 中的StatefulWidget,您可以使用widget.field 访问这些值。

    【讨论】:

    • 您好 Felipe,感谢您的帮助。我仍在努力解决问题。我还附上了关于上述问题的 messageData。非常感谢您的帮助
    • 问题是您没有在代码中创建任何MessageData 对象,所以如果我尝试运行您提供的内容,我不会遇到您的错误。你需要先初始化一个对象。
    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2020-08-10
    • 2019-11-29
    • 2021-08-29
    • 2020-05-06
    • 2020-04-20
    • 2021-07-23
    相关资源
    最近更新 更多