我正在尝试在firebase中应用过滤器,并在结果中使用startswith过滤器。我已经初始化了充当所有类之外的过滤器的变量
int duration = 1440;
String destination = '';
String Company = '';
int Maxcost = 99999999999;
int MinCost = 0;
String cla = '';
String airport = '';
这意味着所有文件都将被允许进入。但是当我从另一个这样的类更改变量时。
过滤器类
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Home(email:email,destination:destination,duration:duration,Maxcost: maxCost,Mincost: minCost,airport: airport,Company: company,)));
正在接收课程(状态控件)
Home({this.email,duration = 1440,destination ='',Company = '',Maxcost = 99999999,Mincost = 0,cla = '',airport = ''});
我添加了“ =”,因为还有其他类导致这种情况,但它们没有给出这些值。这样一来,当这些类不传递这些值时,它将成为默认值而不是null。现在出现问题了
StreamBuilder(
stream: Firestore.instance.collection("Planes").where(
"cost", isLessThan: Maxcost).where(
"cost", isGreaterThan: MinCost).snapshots(),
builder: (context, snapshot) {
List<DocumentSnapshot> m = snapshot.data.documents;
List<DocumentSnapshot> l=[];
DocumentSnapshot i;
for (i in m) {
if (i[' destination'].startsWith(destination) && i['duration']<(duration) && i['airport'].startsWith(airport) && i['class'].startsWith(cla) && i['company'].startsWith(Company)) {
l.add(i);
}
}
return ListView.builder(
itemCount: l.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 1.0, horizontal: 4.0),
child: Card(
child: ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) =>
ViewPlane(plane: snapshot,
index: index,
email: email,)));
},
leading: Icon(Icons.airplanemode_active),
title: Text(l[index][' destination']),
subtitle: Text(
l[index]['airport']),
),
),
);
},
);
}
),
在发生这种情况之前(这是首先要做的事情之一),将变量设置为默认值。怎么来的?变量未在构建函数中初始化。如果需要,这是主类的完整代码。
主要班级
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:discount_app/AddSheets/AddPlane.dart';
import 'package:discount_app/FilterPage/PlaneFilter.dart';
import 'package:discount_app/Headings/home_screen_plane_packages.dart';
import 'package:discount_app/chat_pack/Loading.dart';
import 'package:discount_app/chat_pack/Settings.dart';
import 'package:discount_app/chat_pack/chat_home_page.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:discount_app/ViewSheets/ViewPlane.dart';
import 'package:discount_app/login_signup/Login.dart';
import 'package:discount_app/chat_pack/Const.dart';
import 'package:google_sign_in/google_sign_in.dart';
List<Choice> choices = const <Choice>[
const Choice(title:"Filters",icon:Icons.filter),
const Choice(title: 'Settings', icon: Icons.settings),
const Choice(title: 'Log out', icon: Icons.exit_to_app),
];
int duration = 1440;
String destination = '';
String Company = '';
int Maxcost = 99999999999;
int MinCost = 0;
String cla = '';
String airport = '';
@override
class Home extends StatefulWidget {
String email;
Home({this.email,duration = 1440,destination ='',Company = '',Maxcost = 99999999,Mincost = 0,cla = '',airport = ''});
@override
_HomeState createState() => _HomeState(email:email);
}
class _HomeState extends State<Home> {
var s;
void onItemMenuPress(Choice choice,) {
if (choice.title == 'Log out') {
handleSignOut();
}
else if (choice.title == 'Filters') {
Navigator.push(
context, MaterialPageRoute(builder: (context) => planeFilter(email:email)));
}
else {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Settings()));
}
}
final GoogleSignIn googleSignIn = GoogleSignIn();
bool isLoading = false;
Future<Null> openDialog() async {
switch (await showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
contentPadding: EdgeInsets.only(
left: 0.0, right: 0.0, top: 0.0, bottom: 0.0),
children: <Widget>[
Container(
color: themeColor,
margin: EdgeInsets.all(0.0),
padding: EdgeInsets.only(bottom: 10.0, top: 10.0),
height: 100.0,
child: Column(
children: <Widget>[
Container(
child: Icon(
Icons.exit_to_app,
size: 30.0,
color: Colors.white,
),
margin: EdgeInsets.only(bottom: 10.0),
),
Text(
'Exit app',
style: TextStyle(color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
Text(
'Are you sure to exit app?',
style: TextStyle(color: Colors.white70, fontSize: 14.0),
),
],
),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, 0);
},
child: Row(
children: <Widget>[
Container(
child: Icon(
Icons.cancel,
color: primaryColor,
),
margin: EdgeInsets.only(right: 10.0),
),
Text(
'CANCEL',
style: TextStyle(
color: primaryColor, fontWeight: FontWeight.bold),
)
],
),
),
SimpleDialogOption(
onPressed: () {
Navigator.pop(context, 1);
},
child: Row(
children: <Widget>[
Container(
child: Icon(
Icons.check_circle,
color: primaryColor,
),
margin: EdgeInsets.only(right: 10.0),
),
Text(
'YES',
style: TextStyle(
color: primaryColor, fontWeight: FontWeight.bold),
)
],
),
),
],
);
})) {
case 0:
break;
case 1:
exit(0);
break;
}
}
Future<Null> handleSignOut() async {
this.setState(() {
isLoading = true;
});
await FirebaseAuth.instance.signOut();
await googleSignIn.disconnect();
await googleSignIn.signOut();
this.setState(() {
isLoading = false;
});
Navigator.of(context)
.pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => LoginScreen()), (
Route<dynamic> route) => false);
}
String email;
_HomeState({this.email});
@override
Widget build(BuildContext context) {
return GestureDetector(
onPanUpdate: (details) {
if (details.delta.dx > 0) {
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => PlanePackages(email: email,)));
}
},
child: new Scaffold(
backgroundColor: Colors.pink[800],
appBar: new AppBar(
title: new Text('Home page'),
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) =>
HomeScreen(currentUserId: email,)));
},
child: Icon(Icons.chat),
)),
PopupMenuButton<Choice>(
onSelected: onItemMenuPress,
itemBuilder: (BuildContext context) {
return choices.map((Choice choice) {
return PopupMenuItem<Choice>(
value: choice,
child: Row(
children: <Widget>[
Icon(
choice.icon,
color: primaryColor,
),
Container(
width: 10.0,
),
Text(
choice.title,
style: TextStyle(color: primaryColor),
),
],
));
}).toList();
},
),
],
),
body: WillPopScope(
child: Stack(
children: <Widget>[ StreamBuilder(
stream: Firestore.instance.collection("Planes").where(
"cost", isLessThan: Maxcost).where(
"cost", isGreaterThan: MinCost).snapshots(),
builder: (context, snapshot) {
List<DocumentSnapshot> m = snapshot.data.documents;
List<DocumentSnapshot> l=[];
DocumentSnapshot i;
for (i in m) {
if (i[' destination'].startsWith(destination) && i['duration']<(duration) && i['airport'].startsWith(airport) && i['class'].startsWith(cla) && i['company'].startsWith(Company)) {
l.add(i);
}
}
return ListView.builder(
itemCount: l.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 1.0, horizontal: 4.0),
child: Card(
child: ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) =>
ViewPlane(plane: snapshot,
index: index,
email: email,)));
},
leading: Icon(Icons.airplanemode_active),
title: Text(l[index][' destination']),
subtitle: Text(
l[index]['airport']),
),
),
);
},
);
}
),
Positioned(
child: isLoading ? const Loading() : Container(),
)
]
),
onWillPop: () async {
openDialog();
return Future.value(false);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
print(cla);
Navigator.push(context, MaterialPageRoute(
builder: (context) => AddPlane(email: email,)));
},
backgroundColor: Colors.green,
child: Icon(
Icons.add,
size: 30,
),
),
),
);
}
}
任何帮助将不胜感激
答案 0 :(得分:0)
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Home(email:email,destationName:destination,durationValue:duration,maximumCost: maxCost,minimumCost: minCost,airportName: airport,CompanyName: company,)));
以这种方式进行分配,因为它们是全局变量,您不能直接在构造函数参数中分配
Home({this.email,durationValue = 1440,destinationName ='',companyName = '',maximumCost = 99999999,minimumCost = 0,claValue = '',airportName = ''})
{
duration=durationValue;
destination=destinationName;
Maxcost = maximumCost;
Mincost = minimmCost;
cla=claValue;
airport=airportName;
Company = companyName;
}