0

Stream Builder 不起作用:我希望它在用户登录时移至下一页。

我收到了用于登录但未由 Stream Builder 执行导航的电子邮件的令牌。

这是 Main.dart:

class _ThisAppState extends State<ThisApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: _handleWindowDisplay(),
      ),
    );
  }
}
Widget _handleWindowDisplay() {
  StreamBuilder(
      stream: FirebaseAuth.instance.onAuthStateChanged,
      builder: (context, asyncSnapshot) {
        if (asyncSnapshot.hasError) {
          return new Text("Error!");
        } else if (asyncSnapshot.data == null) {
          return new LoginPage();
        } else {
          if (asyncSnapshot.data.state) {
            return new LoginPage();
          } else
            return MainScreen();
        }
      },);
}


class _ThisAppState extends State<ThisApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: _handleWindowDisplay(),
      ),
    );
  }
}
Widget _handleWindowDisplay() {
  StreamBuilder(
      stream: FirebaseAuth.instance.onAuthStateChanged,
      builder: (context, asyncSnapshot) {
        if (asyncSnapshot.hasError) {
          return new Text("Error!");
        } else if (asyncSnapshot.data == null) {
          return new LoginPage();
        } else {
          if (asyncSnapshot.data.state) {
            return new LoginPage();
          } else
            return MainScreen();
        }
      },);
}


**Login Screen**

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String _email,_Password;

  FutureOr Function(AuthResult value) get onValue => null;
  @override
  Widget build(BuildContext context) {
    return Container(
      child:
        Form(child:
        Column(
          children: <Widget>[
            TextFormField(decoration: InputDecoration(labelText: "Enter Email"),
            onChanged: (value){
              this.setState((){_email=value;});
            },
            ),
            SizedBox(height: 10,),
            TextFormField(decoration: InputDecoration(labelText: "Enter Password"),
            onChanged:(value) {
              this.setState(() {
                _Password = value;
              });
            },
            ),
            SizedBox(height: 10,),
            RaisedButton(child: Text("Sign In"),onPressed: ()=>{
              FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _Password)
            .then((onValue){

              }).catchError((error){
              debugPrint("The error is "+error);
            })
            })
    ],

        ),
      ),
    );
  }
}
4

2 回答 2

0

您需要使用 Navigator.push() 方法。

例如,要移动到 MainScreen(),您需要使用以下代码。

Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => MainScreen()),
  );

您应该查看导航基础知识

于 2019-10-21T13:28:13.383 回答
0

我认为 _handleWindowDisplay() 应该返回 streambuilder 而不仅仅是 StreamBuilder 返回的小部件

Widget _handleWindowDisplay() {
 return StreamBuilder(
  stream: FirebaseAuth.instance.onAuthStateChanged,
  builder: (context, asyncSnapshot) {
    if (asyncSnapshot.hasError) {
      return new Text("Error!");
    } else if (asyncSnapshot.data == null) {
      return new LoginPage();
    } else {
      if (asyncSnapshot.data.state) {
        return new LoginPage();
      } else
        return MainScreen();
    }
  },);
}
于 2020-10-06T15:56:31.527 回答