【问题标题】:Flutter authentification with streams使用流进行 Flutter 身份验证
【发布时间】:2020-01-05 13:11:21
【问题描述】:

显然我在这里做错了什么......登录后,我将新的布尔值(T)发送到 isAuthorized 流。 StreamBuilder 重新运行并最终执行正确的 if-else 分支,但由于某种原因,Login Widget 仍然呈现在屏幕上?

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      theme: appTheme,
      initialRoute: '/',
      home: StreamBuilder<bool>(
        stream: getIt.get<SessionBloc>().isAuthorized,
        initialData: false,
        builder: (context, snapshot) {
          if (snapshot.data) {
            return Home();
          } else {
            return Login();
          }
        },
      ),
      routes: {
          '/': (context) => Home(),
          '/profile': (context) => Profile(),
          '/login': (context) => Login(),
          '/register': (context) => Register(),
        },
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart bloc rxdart


    【解决方案1】:

    因为我不知道你的集团在做什么,所以我创建了一个模仿集团的简单流。使用此代码 sn-p 可以正常工作。如果该集团为您提供了您想要的东西并使用了适当的 if-else 块,那么您应该考虑删除'/': (context) =&gt; Home()

    这是我有回家路线时的例外: 如果指定了 home 属性,则路由表不能包含“/”条目,因为它是多余的。

     class Authorization {
          final _authorization = StreamController<bool>();
          StreamSink<bool> get send => _authorization.sink;
          Stream<bool> get isAuthorized => _authorization.stream;
          get close => _authorization.close();
        }
    
    class MyApp extends StatelessWidget {
      Authorization a = new Authorization();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'App',
          home: StreamBuilder<bool>(
            stream: a.isAuthorized,
            initialData: false,
            builder: (context, snapshot) {
              if (snapshot.data) {
                return Home(auth: a);
              } else {
                return Login(auth: a);
              }
            },
          ),
          routes: {
            '/login': (context) => Login(auth: a),
          },
        );
      }
    }
    
    class Home extends StatelessWidget {
      final Authorization _auth;
    
      Home({@required Authorization auth})
          : assert(auth != null),
            _auth = auth;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[Text("Home"), RaisedButton(
              onPressed: ()  {
                _auth.send.add(false);
              },
            )],
          ),
        );
      }
    }
    
    class Login extends StatelessWidget {
    
      final Authorization _auth;
    
      Login({@required Authorization auth})
          : assert(auth != null),
            _auth = auth;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[Text("Login"), RaisedButton(
              onPressed: ()  {
                _auth.send.add(true);
              },
            )],
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2021-04-14
      • 2020-12-04
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多