【问题标题】:Named routes with arguments带参数的命名路由
【发布时间】:2020-11-17 19:31:18
【问题描述】:

我在命名路线方面遇到了一些困难。

我正在尝试让一些 Firebase 数据在 onPress 保存后显示在屏幕上。

  Widget build(BuildContext context) {

DocumentSnapshot document;
final thisContact = Contact.fromSnapshot(document);

...

routes: <String, WidgetBuilder>{ 
'/second': (BuildContext context) => ViewContact(contact: thisContact)

.

Contact.fromSnapshot(DocumentSnapshot snapshot)
  : name = snapshot.data()['Name'],
    phoneNumber = snapshot.data()['PhoneNumber'],
    location = snapshot.data()['Location'],
    rating = snapshot.data()['Rating'],
    instagram = snapshot.data()['Instagram'],
    birthday = snapshot.data()['Birthday'],
    notes = snapshot.data()['Notes'];

保存页面

onPressed: () async {
              final uid =
                  await TheProvider.of(context).auth.getCurrentUID();

              widget.contact.name = oneController.text;
              widget.contact.phoneNumber = int.tryParse(twoController.text);
              widget.contact.location = threeController.text;
              widget.contact.rating = int.tryParse(fourController.text);
              widget.contact.instagram = fiveController.text;
              widget.contact.birthday = int.tryParse(sixController.text);
              widget.contact.notes = sevenController.text;

              await db
                  .collection("userData")
                  .doc(uid)
                  .collection("Contacts")
                  .add(widget.contact.toJson());

              Navigator.pushReplacementNamed(context, "/second");
            })
      ],

这会导致以下错误:

The method 'data' was called on null. Receiver: null Tried calling: data()

最好的方法是什么?

【问题讨论】:

  • 可以分享代码相关的“数据”。
  • @Akif 刚刚添加
  • 文档为空。在传递给 Contact.fromSnapshot() 之前,您没有更新它
  • @Lee3 对,但如果我之后通过 MaterialPageRoute 访问该页面,它确实有效
  • 从您设置文档值的页面共享代码。我认为您的构造函数可能不正确。

标签: flutter dart


【解决方案1】:

首先,从ViewContact 构造函数中删除参数,因为我们将使用路由参数。

routes: <String, WidgetBuilder>{ 
  '/second': (BuildContext context) => ViewContact()

Contact 作为参数传递给Navigator.pushReplacementNamed()

onPressed: () async {
              ...
          Navigator.pushReplacementNamed(context, "/second", arguments: Contact.fromSnapshot(document));
        })
  ],

ViewContactbuild方法中,或者initState如果是StatefulWidget

  Contact contact = ModalRoute.of(context).settings.arguments;

【讨论】:

  • 感谢李,你一直很有帮助。我刚刚实现了这一点,现在我得到了Too many positional arguments: 2 expected, but 3 foundNavigator.pushReplacementNamedViewContact 构造函数也说 The parameter 'contact' is required - 当然,如果它正在工作,可以忽略它。
  • 对不起,我留下了 arguments 参数名称。答案已更新。
  • 另外,从ViewContact 类中删除contact 字段,以及从ViewContact 类中的ViewContact 构造函数中删除contact 命名参数。
  • 好的,我已经从 ViewContact 类中删除了 final Contact contact;ViewContact({Key key, @required this.contact}) : super(key: key);。当我单击查看联系人时,它会显示The getter 'location' was called on null. Receiver: null Tried calling: location,即使位置不为空。它也不会指向onPress 上的ViewContact 页面。你知道是什么原因造成的吗?
  • 尝试传递widget.contact 作为路由参数。
猜你喜欢
  • 2013-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
  • 2011-12-23
相关资源
最近更新 更多