Stream Builder无法正常工作,导航错误

时间:2019-10-21 11:35:52

标签: flutter flutter-layout flutter-dependencies flutter-test stream-builder

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);
            })
            })
    ],

        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

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

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

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

您应该结帐Navigation Basics

答案 1 :(得分: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();
    }
  },);
}