我正在尝试使用 generateRoute 将多个参数传递给颤振屏幕
I am trying to pass multiple arguments to a flutter screen by using generateRoute
我正在尝试按照 中显示的方式执行上述操作。
我基本上想传递 2 个参数:docid 和 isCaregiver 到主屏幕,但它显示错误,没有为 class 定义参数。 class ScreenArguments 是传递 2 个参数的 class。请看代码。
ScreenArguments.dart
class ScreenArguments {
final String docid;
final bool isCaretaker;
ScreenArguments({this.docid, this.isCaretaker});
}
main.dart
import 'dart:ui';
import 'package:vitality/stream.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:vitality/screens/login.dart';
import 'screens/homescreen.dart';
import 'screens/chatbot.dart';
import 'screens/todo.dart';
import 'package:vitality/screens/welcome.dart';
import 'screens/register.dart';
import 'package:firebase_core/firebase_core.dart';
import 'components/ScreenArguments.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class ScreenArguments {
final String reportTitle;
final String reportFullPath;
ScreenArguments(this.reportTitle, this.reportFullPath);
}
class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case Welcome.id:
return MaterialPageRoute(builder: (_) => Welcome());
case HomeScreen.id:
return MaterialPageRoute(builder: (BuildContext context) {
ScreenArguments argument = args;
return HomeScreen(
docid: argument.docid,
isCaretaker: argument.isCaretaker,
);
});
case LoginScreen.id:
return MaterialPageRoute(builder: (_) => LoginScreen());
case Register.id:
return MaterialPageRoute(builder: (_) => Register());
case ChatBot.id:
return MaterialPageRoute(builder: (_) => ChatBot());
case Todo.id:
return MaterialPageRoute(builder: (_) => Todo());
}
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Color(0xff222831),
accentColor: Color(0xff00adb5),
textTheme: TextTheme(
headline1: TextStyle(
fontSize: 15.0, fontFamily: 'Montserrat', color: Colors.black),
headline2: TextStyle(
fontSize: 60.0, fontFamily: 'CrimsonText', color: Colors.black),
headline3: TextStyle(
fontSize: 30.0, fontFamily: 'Lora', color: Colors.white),
headline4: TextStyle(
fontSize: 30.0, fontFamily: 'Montserrat', color: Colors.white),
),
),
initialRoute: Welcome.id,
routes: {
'test': (context) => Test(),
Welcome.id: (context) => Welcome(),
HomeScreen.id: (context) => HomeScreen(),
LoginScreen.id: (context) => LoginScreen(),
Register.id: (context) => Register(),
ChatBot.id: (context) => ChatBot(),
Todo.id: (context) => Todo()
},
);
}
}
login.dart
Button(
text: 'LOGIN AS INFIRM ',
onPressed: () {
doesElderlyExist();
print('docid after elderly function call: $docid');
if (docid == null) {
print('user does not exist');
} else {
isCaretaker = false;
Navigator.pushNamed(context, HomeScreen.id,
arguments: ScreenArguments(docid, isCaretaker));
}
}),
main.dart 中显示的错误
login.dart 中显示的错误
generateRoute
函数不知道您的参数是 ScreenArguments
类型,您应该明确定义:
...
case HomeScreen.id:
return MaterialPageRoute(builder: (BuildContext context) {
final argument = args as ScreenArguments;
return HomeScreen(
docid: argument.docid,
isCaretaker: argument.isCaretaker,
);
});
...
还有一件事是您在 main.dart
中定义了另一个同名 ScreenArguments
的 class,仔细检查并删除未使用的那个(根据您的示例,具有 reportTitle
和 reportFullPath
属性的是“错误的”。
[![错误][1]][1]
这是我的错误 login.dart
[1]: https://i.stack.imgur.com/hLjUV.png
这是来自 main.dart 和 route.dart
的代码
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:vitality/screens/welcome.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:vitality/components/route.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Color(0xff222831),
accentColor: Color(0xff00adb5),
textTheme: TextTheme(
headline1: TextStyle(
fontSize: 15.0, fontFamily: 'Montserrat', color: Colors.black),
headline2: TextStyle(
fontSize: 60.0, fontFamily: 'CrimsonText', color: Colors.black),
headline3: TextStyle(
fontSize: 30.0, fontFamily: 'Lora', color: Colors.white),
headline4: TextStyle(
fontSize: 30.0, fontFamily: 'Montserrat', color: Colors.white),
),
),
initialRoute: Welcome.id,
onGenerateRoute: RouteGen.generateRoute,
// routes: {
// 'test': (context) => Test(),
// Welcome.id: (context) => Welcome(),
// HomeScreen.id: (context) => HomeScreen(),
// LoginScreen.id: (context) => LoginScreen(),
// Register.id: (context) => Register(),
// ChatBot.id: (context) => ChatBot(),
// Todo.id: (context) => Todo()
// },
);
}
}
route.dart
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:vitality/screens/login.dart';
import 'package:vitality/screens/homescreen.dart';
import 'package:vitality/screens/chatbot.dart';
import 'package:vitality/screens/todo.dart';
import 'package:vitality/screens/welcome.dart';
import 'package:vitality/screens/register.dart';
import 'package:vitality/components/ScreenArguments.dart';
class RouteGen {
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case Welcome.id:
return MaterialPageRoute(builder: (_) => Welcome());
case HomeScreen.id:
return MaterialPageRoute(builder: (BuildContext context) {
final argument = args as ScreenArguments;
return HomeScreen(
docid: argument.docid,
isCaretaker: argument.isCaretaker,
);
});
case LoginScreen.id:
return MaterialPageRoute(builder: (_) => LoginScreen());
case Register.id:
return MaterialPageRoute(builder: (_) => Register());
case ChatBot.id:
return MaterialPageRoute(builder: (_) => ChatBot());
case Todo.id:
return MaterialPageRoute(builder: (_) => Todo());
}
}
}
login.dart
import 'package:flutter/material.dart';
import 'package:delayed_display/delayed_display.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:vitality/components/ScreenArguments.dart';
import 'package:vitality/screens/homescreen.dart';
import 'package:vitality/components/button.dart';
import 'package:vitality/components/ScreenArguments.dart';
bool isCaretaker = false;
String elderly;
String caretaker;
String carepass;
var docid;
CollectionReference main = FirebaseFirestore.instance.collection('maindb');
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
static const String id = 'login_screen';
}
// class arguments {
// String id;
// bool isCare= false;
// arguments({this.id,this.isCare})
// }
doesCTExist() async {
print('in does caretaker exist');
main.where('caretaker', isEqualTo: caretaker).get().then(
(QuerySnapshot snapshot) => {
snapshot.docs.forEach((f) {
docid = f.reference.id;
}),
},
);
print('docid in caretaker function: $docid');
}
doesElderlyExist() async {
print('in does elderly exist');
print(elderly);
main.where('elderly', isEqualTo: elderly).get().then(
(QuerySnapshot snapshot) => {
snapshot.docs.forEach((f) {
docid = f.reference.id;
}),
},
);
print('docid in elderly function: $docid');
}
class _LoginScreenState extends State<LoginScreen> {
final _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF602247),
toolbarHeight: 50.0,
centerTitle: true,
title: Text(
'VITALITY',
style: Theme.of(context).textTheme.headline4,
)),
backgroundColor: Colors.white,
body: Container(
height: 1000,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://www.fonewalls.com/wp-content/uploads/2019/10/Gradient-Background-Wallpaper-002-300x585.jpg'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.7), BlendMode.dstATop),
)),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
DelayedDisplay(
delay: Duration(seconds: 1),
child: Column(children: <Widget>[
SizedBox(height: 90),
Text('INFIRM LOGIN',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 20.0)),
SizedBox(height: 15.0),
Text('USERNAME',
style: Theme.of(context).textTheme.headline1),
SizedBox(height: 15.0),
TextField(
onChanged: (value) {
elderly = value;
},
),
SizedBox(height: 25.0),
Button(
text: 'LOGIN AS INFIRM ',
onPressed: () {
doesElderlyExist();
print('docid after elderly function call: $docid');
if (docid == null) {
print('user does not exist');
} else {
isCaretaker = false;
// Navigator.pushNamed(context, HomeScreen.id,
// arguments: docid, isCaretaker);
**Navigator.of(context).pushNamed('home_screen',arguments: docid,isCaretaker);**
}
}),
SizedBox(height: 95.0),
Text('CARETAKER LOGIN',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 20.0)),
SizedBox(height: 15.0),
Text('USERNAME',
style: Theme.of(context).textTheme.headline1),
TextField(
onChanged: (value) {
caretaker = value;
},
),
SizedBox(height: 15.0),
Text('PASSWORD',
style: Theme.of(context).textTheme.headline1),
TextField(
onChanged: (value) {
carepass = value;
},
),
SizedBox(height: 25.0),
Button(
text: 'LOGIN AS CARETAKER',
onPressed: () async {
try {
doesCTExist();
print('docid after ct function call: $docid');
final user = await _auth.signInWithEmailAndPassword(
email: caretaker, password: carepass);
if (user != null) {
isCaretaker = true;
Navigator.pushNamed(context, HomeScreen.id,
arguments: docid);
}
} catch (e) {
print(e);
}
},
),
]),
),
],
),
),
),
),
);
}
}
我已经给出错的行加了星标(同截图)
我正在尝试按照
我基本上想传递 2 个参数:docid 和 isCaregiver 到主屏幕,但它显示错误,没有为 class 定义参数。 class ScreenArguments 是传递 2 个参数的 class。请看代码。
ScreenArguments.dart
class ScreenArguments {
final String docid;
final bool isCaretaker;
ScreenArguments({this.docid, this.isCaretaker});
}
main.dart
import 'dart:ui';
import 'package:vitality/stream.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:vitality/screens/login.dart';
import 'screens/homescreen.dart';
import 'screens/chatbot.dart';
import 'screens/todo.dart';
import 'package:vitality/screens/welcome.dart';
import 'screens/register.dart';
import 'package:firebase_core/firebase_core.dart';
import 'components/ScreenArguments.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class ScreenArguments {
final String reportTitle;
final String reportFullPath;
ScreenArguments(this.reportTitle, this.reportFullPath);
}
class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case Welcome.id:
return MaterialPageRoute(builder: (_) => Welcome());
case HomeScreen.id:
return MaterialPageRoute(builder: (BuildContext context) {
ScreenArguments argument = args;
return HomeScreen(
docid: argument.docid,
isCaretaker: argument.isCaretaker,
);
});
case LoginScreen.id:
return MaterialPageRoute(builder: (_) => LoginScreen());
case Register.id:
return MaterialPageRoute(builder: (_) => Register());
case ChatBot.id:
return MaterialPageRoute(builder: (_) => ChatBot());
case Todo.id:
return MaterialPageRoute(builder: (_) => Todo());
}
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Color(0xff222831),
accentColor: Color(0xff00adb5),
textTheme: TextTheme(
headline1: TextStyle(
fontSize: 15.0, fontFamily: 'Montserrat', color: Colors.black),
headline2: TextStyle(
fontSize: 60.0, fontFamily: 'CrimsonText', color: Colors.black),
headline3: TextStyle(
fontSize: 30.0, fontFamily: 'Lora', color: Colors.white),
headline4: TextStyle(
fontSize: 30.0, fontFamily: 'Montserrat', color: Colors.white),
),
),
initialRoute: Welcome.id,
routes: {
'test': (context) => Test(),
Welcome.id: (context) => Welcome(),
HomeScreen.id: (context) => HomeScreen(),
LoginScreen.id: (context) => LoginScreen(),
Register.id: (context) => Register(),
ChatBot.id: (context) => ChatBot(),
Todo.id: (context) => Todo()
},
);
}
}
login.dart
Button(
text: 'LOGIN AS INFIRM ',
onPressed: () {
doesElderlyExist();
print('docid after elderly function call: $docid');
if (docid == null) {
print('user does not exist');
} else {
isCaretaker = false;
Navigator.pushNamed(context, HomeScreen.id,
arguments: ScreenArguments(docid, isCaretaker));
}
}),
main.dart 中显示的错误
login.dart 中显示的错误
generateRoute
函数不知道您的参数是 ScreenArguments
类型,您应该明确定义:
...
case HomeScreen.id:
return MaterialPageRoute(builder: (BuildContext context) {
final argument = args as ScreenArguments;
return HomeScreen(
docid: argument.docid,
isCaretaker: argument.isCaretaker,
);
});
...
还有一件事是您在 main.dart
中定义了另一个同名 ScreenArguments
的 class,仔细检查并删除未使用的那个(根据您的示例,具有 reportTitle
和 reportFullPath
属性的是“错误的”。
[![错误][1]][1]
这是我的错误 login.dart [1]: https://i.stack.imgur.com/hLjUV.png
这是来自 main.dart 和 route.dart
的代码import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:vitality/screens/welcome.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:vitality/components/route.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Color(0xff222831),
accentColor: Color(0xff00adb5),
textTheme: TextTheme(
headline1: TextStyle(
fontSize: 15.0, fontFamily: 'Montserrat', color: Colors.black),
headline2: TextStyle(
fontSize: 60.0, fontFamily: 'CrimsonText', color: Colors.black),
headline3: TextStyle(
fontSize: 30.0, fontFamily: 'Lora', color: Colors.white),
headline4: TextStyle(
fontSize: 30.0, fontFamily: 'Montserrat', color: Colors.white),
),
),
initialRoute: Welcome.id,
onGenerateRoute: RouteGen.generateRoute,
// routes: {
// 'test': (context) => Test(),
// Welcome.id: (context) => Welcome(),
// HomeScreen.id: (context) => HomeScreen(),
// LoginScreen.id: (context) => LoginScreen(),
// Register.id: (context) => Register(),
// ChatBot.id: (context) => ChatBot(),
// Todo.id: (context) => Todo()
// },
);
}
}
route.dart
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:vitality/screens/login.dart';
import 'package:vitality/screens/homescreen.dart';
import 'package:vitality/screens/chatbot.dart';
import 'package:vitality/screens/todo.dart';
import 'package:vitality/screens/welcome.dart';
import 'package:vitality/screens/register.dart';
import 'package:vitality/components/ScreenArguments.dart';
class RouteGen {
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case Welcome.id:
return MaterialPageRoute(builder: (_) => Welcome());
case HomeScreen.id:
return MaterialPageRoute(builder: (BuildContext context) {
final argument = args as ScreenArguments;
return HomeScreen(
docid: argument.docid,
isCaretaker: argument.isCaretaker,
);
});
case LoginScreen.id:
return MaterialPageRoute(builder: (_) => LoginScreen());
case Register.id:
return MaterialPageRoute(builder: (_) => Register());
case ChatBot.id:
return MaterialPageRoute(builder: (_) => ChatBot());
case Todo.id:
return MaterialPageRoute(builder: (_) => Todo());
}
}
}
login.dart
import 'package:flutter/material.dart';
import 'package:delayed_display/delayed_display.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:vitality/components/ScreenArguments.dart';
import 'package:vitality/screens/homescreen.dart';
import 'package:vitality/components/button.dart';
import 'package:vitality/components/ScreenArguments.dart';
bool isCaretaker = false;
String elderly;
String caretaker;
String carepass;
var docid;
CollectionReference main = FirebaseFirestore.instance.collection('maindb');
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
static const String id = 'login_screen';
}
// class arguments {
// String id;
// bool isCare= false;
// arguments({this.id,this.isCare})
// }
doesCTExist() async {
print('in does caretaker exist');
main.where('caretaker', isEqualTo: caretaker).get().then(
(QuerySnapshot snapshot) => {
snapshot.docs.forEach((f) {
docid = f.reference.id;
}),
},
);
print('docid in caretaker function: $docid');
}
doesElderlyExist() async {
print('in does elderly exist');
print(elderly);
main.where('elderly', isEqualTo: elderly).get().then(
(QuerySnapshot snapshot) => {
snapshot.docs.forEach((f) {
docid = f.reference.id;
}),
},
);
print('docid in elderly function: $docid');
}
class _LoginScreenState extends State<LoginScreen> {
final _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF602247),
toolbarHeight: 50.0,
centerTitle: true,
title: Text(
'VITALITY',
style: Theme.of(context).textTheme.headline4,
)),
backgroundColor: Colors.white,
body: Container(
height: 1000,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://www.fonewalls.com/wp-content/uploads/2019/10/Gradient-Background-Wallpaper-002-300x585.jpg'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.7), BlendMode.dstATop),
)),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
DelayedDisplay(
delay: Duration(seconds: 1),
child: Column(children: <Widget>[
SizedBox(height: 90),
Text('INFIRM LOGIN',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 20.0)),
SizedBox(height: 15.0),
Text('USERNAME',
style: Theme.of(context).textTheme.headline1),
SizedBox(height: 15.0),
TextField(
onChanged: (value) {
elderly = value;
},
),
SizedBox(height: 25.0),
Button(
text: 'LOGIN AS INFIRM ',
onPressed: () {
doesElderlyExist();
print('docid after elderly function call: $docid');
if (docid == null) {
print('user does not exist');
} else {
isCaretaker = false;
// Navigator.pushNamed(context, HomeScreen.id,
// arguments: docid, isCaretaker);
**Navigator.of(context).pushNamed('home_screen',arguments: docid,isCaretaker);**
}
}),
SizedBox(height: 95.0),
Text('CARETAKER LOGIN',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 20.0)),
SizedBox(height: 15.0),
Text('USERNAME',
style: Theme.of(context).textTheme.headline1),
TextField(
onChanged: (value) {
caretaker = value;
},
),
SizedBox(height: 15.0),
Text('PASSWORD',
style: Theme.of(context).textTheme.headline1),
TextField(
onChanged: (value) {
carepass = value;
},
),
SizedBox(height: 25.0),
Button(
text: 'LOGIN AS CARETAKER',
onPressed: () async {
try {
doesCTExist();
print('docid after ct function call: $docid');
final user = await _auth.signInWithEmailAndPassword(
email: caretaker, password: carepass);
if (user != null) {
isCaretaker = true;
Navigator.pushNamed(context, HomeScreen.id,
arguments: docid);
}
} catch (e) {
print(e);
}
},
),
]),
),
],
),
),
),
),
);
}
}
我已经给出错的行加了星标(同截图)