【问题标题】:how to show different pages to different types of users in flutter [closed]如何在颤动中向不同类型的用户显示不同的页面[关闭]
【发布时间】:2020-12-28 10:54:30
【问题描述】:

我想根据用户类型显示 3 种类型的页面 - 客户类型的“MyPage”,专业人士类型的“MyPagePro”和新用户打开应用程序时的“Choice”类型。但是,我的以下代码在“snapshot.document”部分显示错误。我不知道应该纠正什么。请帮忙。

 class Wrapper extends StatelessWidget {
 @override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
Future snapshot = Firestore.instance
    .collection('crews')
    .document(user.uid)
    .collection('pros')
    .getDocuments();

if (user == null) {
  return Choice();
} else if (snapshot.documents.length == 0) {
  return MyPage();
} else {
  return MyPagePro();
  }
 }
}

我按照 'lenz' 的建议使用了 Future Builder,我的问题得到了解决,尽管在转到我想要的页面之前,红屏显示了几秒钟。谢谢,lenz!

class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
final uid = user.uid;
return FutureBuilder(
    future: Firestore.instance
        .collection('crews')
        .document(user.uid)
        .collection('pros')
        .getDocuments(),
    builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
      if (user == null) {
        return Choice();
      } else if (snapshot.documents.length == 0) {
        return MyPage();
      } else {
        return MyPagePro();
      }
    });

【问题讨论】:

  • 仍然没有写出当前代码的问题所在。上一个问题被关闭可能是因为您没有花时间写一个好的问题,但您希望有人花时间写一个好的答案。
  • 我编辑了我的问题。我忘了写一个问题,现在我添加了。
  • 我的回答有用吗?如果不让我知道
  • 最好发布错误代码并避免使用“我在那儿遇到错误”的格式。完整的错误消息包含有助于您(和其他人)调试的特定信息(文件名、行号、潜在修复...)。添加完整的错误消息以帮助我们与您一起调试。
  • 我发布了一个新代码,仍然有错误。

标签: flutter google-cloud-platform google-cloud-firestore


【解决方案1】:

您的问题是您正在同步使用 Future 。 Future 需要先从服务器获取数据,然后才能使用它。这不会立即发生。

一种解决方案是使用FutureBuilder。这使您可以同步访问 Future 的响应。首先,您的 Future 将没有数据(下面的 else 场景)。那么当服务器响应时,要么有数据,要么有错误。

试试这样的:

class Wrapper extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<CollectionReference>(
    future: Firestore.instance.collection('pros').getDocuments(),
    builder: (BuildContext context, AsyncSnapshot<CollectionReference> snapshot) {
      if (snapshot.hasData) {
        // return either the MyPage or Authenticate widget
        if (user == null) {
          return Choice();
        } else if (snapshot.documents.length == 0) {
          return MyPage();
        } else {
          return MyPagePro();
        }
      } else if (snapshot.hasError) {
        //TODO if Future resolves with error case
      } else {
        //TODO While Future is loading case
        // e.g.:
        return Text('Awaiting result...'),
      }
    },
  )
}

【讨论】:

  • 请注意,我不能 100% 确定 CollectionReference 是否是正确的数据类型。如果不只是用正确的返回类型替换它。 Firestore.instance.collection('pros').getDocuments() 返回的那个。抱歉,我现在没有在电脑上确认这一点。
  • ....要获取该信息,您只需将鼠标悬停在 VSCode 或 AndroidStudio 中的 .getDocument() 方法上即可。您也可以完全删除 。虽然它会给你更少的智能感知
  • 如果我的回答有用,请花点时间点赞并采纳
  • 我听从了您的建议,但您的代码有更多错误。感谢您的宝贵时间,但这不是解决方案
  • 这肯定是您发布的问题的解决方案。如果您遇到不同的错误,也发布它们
猜你喜欢
  • 2021-11-13
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 2021-04-07
  • 2014-11-19
  • 1970-01-01
  • 2020-06-21
相关资源
最近更新 更多