以下是要检查的相关代码段(我从更复杂的文件中添加了这些代码段,并希望我输入的所有内容正确无误):
Main.dart
import 'package:flutter/material.dart';
import 'loginView.dart';
import 'checkinView.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => LoginView(),
'/checkin': (BuildContext context) => CheckinView(),
}
);
}
}
LoginView.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginView extends StatefulWidget {
@override
LoginViewState createState() => LoginViewState();
}
class LoginViewState extends State<LoginView> {
@override
void initState() {
super.initState();
checkLoginAndRedirect();
}
void checkLoginAndRedirect() async {
FirebaseAuth.instance.currentUser().then((FirebaseUser currUser){
if(currUser != null){
print("[LoginView - user] $currUser");
Navigator.pushNamed(context, '/checkin', arguments: {currUser});
}
});
}
}
CheckinView.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'checkinList.dart';
class CheckinView extends StatefulWidget {
final FirebaseUser user;
CheckinView({this.user});
@override
CheckinViewState createState() {
print("[CheckinView - user] $user");
return CheckinViewState();
}
}
class CheckinViewState extends State<CheckinView> {
FirebaseUser _currentUser;
@override
void initState() {
super.initState();
setState((){
_currentUser = widget.user;
});
}
@override
Widget build(BuildContext context) {
print("[CheckinViewState - widget.user] " + widget.user.toString());
return Scaffold(
body: new Column(
children: <Widget>[
CheckinList(user: _currentUser)
]
)
);
}
}
CheckinList.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class CheckinList extends StatelessWidget{
FirebaseUser user = null;
CheckinList({this.user = null});
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(30.0),
child: Text(user['name'])
);
}
}
答案 0 :(得分:0)
您需要在ModalRoute
方法内使用build
访问传递的数据,下面的代码将让您知道如何做,
class LevelViewChildScreen extends StatelessWidget {
UserModel user;
@override
Widget build(BuildContext context) {
user = ModalRoute.of(context).settings.arguments;
return LevelViewChild(user);
}
}
class LevelViewChild extends StatefulWidget {
UserModel user;
LevelViewChild(this.user);
答案 1 :(得分:0)
Main.dart
import 'package:flutter/material.dart';
import 'loginView.dart';
import 'checkinView.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => LoginView(),
'/checkin': (BuildContext context) => CheckinView(),
}
);
}
}
LoginView.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginView extends StatefulWidget {
@override
LoginViewState createState() => LoginViewState();
}
class LoginViewState extends State<LoginView> {
@override
void initState() {
super.initState();
checkLoginAndRedirect();
}
void checkLoginAndRedirect() async {
FirebaseAuth.instance.currentUser().then((FirebaseUser currUser){
if(currUser != null){
print("[LoginView - user] $currUser");
//// pass a map as 'arguments' to be ready to pass along more variables in the future
Navigator.pushNamed(context, '/checkin', arguments: {'user': currUser});
}
});
}
}
CheckinView.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'checkinList.dart';
class CheckinView extends StatefulWidget {
//// leave the stateful class as usual
@override
CheckinViewState createState() => CheckinViewState();
}
class CheckinViewState extends State<CheckinView> {
//// don't bother in the initState method as the BuildContext is missing here
@override
Widget build(BuildContext context) {
//// access the passed arguments
Map<String, dynamic> args = ModalRoute.of(context).settings.arguments;
print("[CheckinViewState - widget.user] " + widget.user.toString());
return Scaffold(
body: new Column(
children: <Widget>[
//// access the passed variables through 'args'
CheckinList(user: args.user)
]
)
);
}
}
CheckinList.dart
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class CheckinList extends StatelessWidget{
FirebaseUser user = null;
//// assign the user in your constructor
CheckinList(FirebaseUser user){
this.user = user;
};
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(30.0),
child: Text(user['name'])
);
}
}
在食谱中阅读有关此内容的更多信息:
答案 2 :(得分:0)
您可以尝试。希望它能对您有所帮助
class LoginViewState extends State<LoginView> {
@override
void initState() {
super.initState();
checkLoginAndRedirect();
}
void checkLoginAndRedirect() async {
FirebaseAuth.instance.currentUser().then((FirebaseUser currUser){
if(currUser != null){
print("[LoginView - user] $currUser");
Navigator.pushNamed(context, '/checkin', arguments: currUser); /// <--- fixed here
}
});
}
}
class CheckinView extends StatefulWidget {
RouteSettings settings = ModalRoute.of(context).settings /// <--- fixed here
FirebaseUser user = settings.arguments; /// <--- fixed here
@override
CheckinViewState createState() {
print("[CheckinView - user] $user");
return CheckinViewState();
}
}