Get data from two collections in Firebase flutter - android

I have two collections which are called Users and Employee. I need to get data from specific field from those collections. "Users" and "Employee" collections have the same field like "password". I have been trying to get the password from Users and Employee collections and use it to navigate me different screen . My codes:
QuerySnapshot snap = await FirebaseFirestore.instance
.collection ("Employee").where ('id', isEqualTo: id).get();
QuerySnapshot snap2 = await FirebaseFirestore.instance
.collection ("Users").where ('uid', isEqualTo: id).get();
print(snap.docs[0]['id']);
print(snap2.docs[0]['uid']);
User.employeeId = id;
try {
if (password == snap.docs[0]['password'] ) {
sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString('employeeId', id).then((_){
navigateNext(HomeScreen());
});
}else if (password == snap2.docs[0]['upassword']) {
sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString('userId', id).then((_){
navigateNext(AdminScreen());
});
error:
E/flutter ( 7498): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 7498): #0 List.[] (dart:core-patch/growable_array.dart:264:36)
E/flutter ( 7498): #1 _LoginScreenState.build. (package:attendanceapp/loginscreen.dart:126:38)
E/flutter ( 7498):
E/flutter ( 7498):

I saw there is a collection inside the document(Users or Employee), I am not sure whether you could get them properly.
Here is my code with a similar setup as you posted. If you could not get the data properly, I recommended you to assign the value to variable "id" before getting value from FirebaseFirestore.
I normally desiged a button for debugging. Hope it could help you.
void getMessagesTest222() async{
final id = "1";
QuerySnapshot querySnapshot = await _firestore.collection('User').where('uid', isEqualTo: id).get();
final allData1 = querySnapshot.docs.map((doc) => doc.get('uid')).toList();
final allData2 = querySnapshot.docs.map((doc) => doc.get('upassword')).toList();
print('uid = >$allData1');
print('upassword = > $allData2');
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Record'),
leading: null,
actions: <Widget>[
IconButton(
icon: const Icon(Icons.add),
onPressed: (){
Navigator.pushNamed(context, creationCategory.id);
},
),
IconButton(
icon: const Icon(Icons.settings),
onPressed: () async {
getMessagesTest222();
// await getData();
// await getMessagesTestTest();
// Navigator.push(
// context,
// MaterialPageRoute(builder: (context) => const ItemDetailsScrrent()),
// );
},
),
],
),
body: SafeArea(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Trahs Collection',
style: const TextStyle(
color: Colors.black38, fontWeight: FontWeight.bold, fontSize: 40),
),
ItemStream(),
],
),
),
),
);
}
Fig1.Firebase structure
Fig2.print out the data from firestore
Updated Code for the second question from this user.
As you mentioned, you created two pages that are for different users
We could get the data to make sure whether this account is existing, and then we could create two if loops to login to different pages.
RoundButton(
title: 'Log In',
colour: Colors.lightBlueAccent,
onPressed: () async{
try {
final user = await _auth.signInWithEmailAndPassword(email: email, password: password);
// if (user != null) {
// // Navigator.pushNamed(context, ChatScreen.id);
// Navigator.pushNamed(context, RecordScreen.id);
// }
if (user.user?.email.toString() == 'add#123.com') {
// Navigator.pushNamed(context, ChatScreen.id);
Navigator.pushNamed(context, administrors.id);
}
if (user.user?.email.toString() == 'operator#123.com') {
// Navigator.pushNamed(context, ChatScreen.id);
Navigator.pushNamed(context, user.id);
}
}
catch(e) {
print(e);
}
},
),

Related

There should be exactly one item with [DropdownButton]'s value: 0. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

Please someone help me! I created DropdownButton with map key and values on StatefullWidget.
I have the following code producing an error when I run it
Settings_Form.Dart:
final user = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: DatabaseServices(uid: user.uid).userData,
builder: (context, snapshot) {
if(snapshot.hasData){
UserData? userData = snapshot.data;
return Form(
key: _formKey,
child: Column(
children: [
Text(
'Update your brew settings.',
style: TextStyle(fontSize: 18.0),
),
SizedBox(height: 20),
TextFormField(
initialValue: userData!.name,
decoration: textInputDecoration,
validator: (val) => val!.isEmpty ? 'Please enter a name' : null,
onChanged: (val) => setState(() => _currentName = val),
),
SizedBox(height: 10),
This is where I believe the problem is arising from. The DropdownField has all my troubles nested right in there. I am really stresses out.
DropdownButtonFormField(
value: userData.sugars,
decoration: textInputDecoration,
items: sugars.map((sugar) {
return DropdownMenuItem(
value: sugars,
child: Text('$sugar sugars'),
);
}).toList(),
onChanged: (val) => setState(() => _currentSugars = 'val' ),
),
SizedBox(height: 10),
Slider(
value: (_currentStrength ?? userData.strength).toDouble(),
activeColor: Colors.brown[_currentStrength ?? userData.strength],
inactiveColor: Colors.brown[_currentStrength ?? userData.strength],
min: 100.0,
max: 900.0,
divisions: 8,
onChanged: (val) => setState(() => _currentStrength = val.round()),
),
// slider
RaisedButton(
color: Colors.brown[400],
child: Text(
'Update',
style: TextStyle(color: Colors.white),
),
onPressed: () async {
if(_formKey.currentState!.validate()){
await DatabaseServices(uid: user.uid).updateUserData(
userData.sugars,
userData.name,
userData.strength
);
}
}
),
],
),
);
}else{
return Loading();
}
}
);
}
}
Database Class:
class DatabaseServices {
final String? uid;
DatabaseServices({ this.uid });
//collection reference
final CollectionReference brewCollection = Firestore.instance.collection('brews');
Future updateUserData (String sugars, String name, int strength) async {
return await brewCollection.document(uid).setData({
'sugars' : sugars,
'name' : name,
'strength': strength,
});
}
//get brew list from snapshot
List<Brew> _brewListFromSnapshot (QuerySnapshot snapshot) {
return snapshot.documents.map((doc){
return Brew(
name: doc['name'] ?? '',
sugars: doc['sugars'] ?? 0,
strength: doc['strength'] ?? '0',
);
}).toList();
}
//user data from snapshot
UserData _userDataFromSnapshot(DocumentSnapshot snapshot){
return UserData(
uid: 'uid',
name: snapshot.data['name'],
sugars: snapshot.data['sugars'],
strength: snapshot.data['strength']
);
}
//get brews Stream
Stream <List<Brew>> get brews{
return brewCollection.snapshots()
.map(_brewListFromSnapshot);
}
//get user doc stream
Stream<UserData > get userData {
return brewCollection.document(uid).snapshots()
.map(_userDataFromSnapshot);
}
}
Error Message:
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 1506 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'

Delete data from firebase by id using flutter

I'm new with Flutter. Currently I am trying to do the CRUD. But then I got some error to delete the data by ID. I did manage to do the delete operation but then it will delete the latest inserted data instead, not the data that onTap. Here I attach my source code.
String docId;
#override
Widget build(BuildContext context) {
CollectionReference users = FirebaseFirestore.instance.collection('taks');
DocumentSnapshot ds;
return new StreamBuilder(
stream: users.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
ds = snapshot.data.docs[index];
// children: snapshot.data.docs.map((document) {
return new ListTile(
title: new Text(ds['task']),
subtitle: Wrap(
children: <Widget>[
Text("Priority: " + ds['priority']),
Text(" | Status: " + ds['status']),
],
),
onTap: (){
docId = ds.id;
print(docId);
},
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.update_rounded,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => UpdateScreen(docId)));
}
),
IconButton(
icon: Icon(
Icons.delete_outline,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () async {
try {
FirebaseFirestore.instance
.collection("taks")
.doc(docId)
.delete()
.then((_) {
print("success!");
});
}
catch (e) {
print("ERROR DURING DELETE");
}
// _onDeleteItemPressed(index);
},
),
],
),
// subtitle: new Text(document['priority']),
);
});
// );
},
);
So, I tried to print the docId on which row that been selected. I tap all the data but it will only read the latest data id only.
So can anyone help me to sort out this problem on how to delete the data that been selected only, not always delete the latest data? Thank you in advanced
I'm sure I understand what exactly it is you want to delete, but your function tells Firebase to delete the entire document with the ID you are passing.
You also are defining `String docId' to your whole widget and using it for all your ListView.Builder items.
Try this:
ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
ds = snapshot.data.docs[index];
String docIdTobeDeleted= ds.id;
// children: snapshot.data.docs.map((document) {
return new ListTile(
title: new Text(ds['task']),
subtitle: Wrap(
children: <Widget>[
Text("Priority: " + ds['priority']),
Text(" | Status: " + ds['status']),
],
),
onTap: (){
//you won't be needing this anymore, instead you can type:
print(docIdTobeDeleted);
//docId = ds.id;
//print(docId);
},
and for firebase below, use this:
onPressed: () async {
try {
FirebaseFirestore.instance
.collection("taks")
.doc(docIdTobeDeleted)
.delete()
.then((_) {
print("success!");
});
}
catch (e) {
print("ERROR DURING DELETE");
}
It should work.
your Listtile onTap will set the docID to the selected tileID.. and the deleteIconButton will delete the id of docID.. so if you tap on the first ListTile and tap on any of the deleteIconButton.. It will delete the first ListTile
You can use the direct ds.id instead of docID in the deleteIconButton
IconButton(
icon: Icon(
Icons.delete_outline,
size: 20.0,
color: Colors.brown[900],
),
onPressed: () async {
try {
FirebaseFirestore.instance
.collection("taks")
.doc(ds.Id)
.delete()
.then((_) {
print("success!");
});
}
catch (e) {
print("ERROR DURING DELETE");
}
// _onDeleteItemPressed(index);
},
),

Firebase Basic Query for datetime Flutter

I am trying to write a program to check if the time selected by the user already exists in the firebase firestore or not. If it does then I navigate back to the page where they select time again.
But as of now, I am succeeded in sending the date and time to firebase and but not the latter part.
DateTime _eventDate;
bool processing;
String _time;
bool conditionsStatisfied ;
#override
void initState() {
super.initState();
_eventDate = DateTime.now();
processing = false ;
}
inside showDatePicker()
setState(() {
print('inside the setState of listTile');
_eventDate = picked ;
});
inside the button (SAVE):
onPressed: () async {
if (_eventDate != null) {
final QuerySnapshot result = await FirebaseFirestore
.instance
.collection('events')
.where('event_date', isEqualTo: this._eventDate)
.where('selected_time', isEqualTo: this._time)
.get();
final List <DocumentSnapshot> document = result.docs;
if (document.length > 0) {
setState(() {
print('inside the method matching conditions');
showAlertDialogue(context);
});
}else{
final data = {
// "title": _title.text,
'selected_time ': this._time,
"event_date": this._eventDate
};
if (widget.note != null) {
await eventDBS.updateData(widget.note.id, data);
} else {
await eventDBS.create(data);
}
Navigator.pop(context);
setState(() {
processing = false;
});
}
};
some guidance needed on how do I resolve this issue!
Also, because of the else statement now the program won't write the date into firestore.
After Alot of research, I came to realize that if you send the data from calendar in DateTime format then, because of the timestamp at the end of the Date it becomes impossible to match to dates. Hence I formatted the DateTime value into (DD/MM/YYYY).
Here is the rest of the code for reference:
class _AddEventPageState extends State<AddEventPage> {
String _eventDate;
bool processing;
String _time;
#override
void initState() {
super.initState();
// _eventDate = DateTime.now();
processing = false ;
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Please select a date'),),
body: Column(
children: [
hourMinute30Interval(),
Text('$_time'),
ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
ListTile(
title: Text(
'$_eventDate'),
onTap: () async {
DateTime picked = await showDatePicker(context: context,
initialDate: DateTime.now(),
firstDate: DateTime(DateTime.now().year - 1),
lastDate: DateTime(DateTime.now().year + 10),);
if (picked != null) {
setState(() {
print('inside the setState of listTile');
_eventDate = DateFormat('dd/MM/yyyy').format(picked) ;
});
}
},
),
SizedBox(height: 10.0),
ListTile(
title: Center(
child: Text('Select time for appointment!', style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
),
),
processing
? Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Theme
.of(context)
.primaryColor,
child:MaterialButton(
child: Text('SAVE', style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
)),
onPressed: () async {
if (_eventDate != null) {
AddingEventsUsingRajeshMethod().getAvailableSlots(
_eventDate, _time).then((QuerySnapshot docs) async {
if (docs.docs.length == 1) {
showAlertDialogue(context);
}
else{
final data = {
// "title": _title.text,
'selected_time': this._time,
"event_date": _eventDate,
};
if (widget.note != null) {
await eventDBS.updateData(widget.note.id, data);
} else {
await eventDBS.create(data);
}
Navigator.pop(context);
setState(() {
processing = false;
});
}
});
}
}
),
),
),
],
),
],
),
);
}
showAlertDialogue method :
showAlertDialogue(BuildContext context) {
Widget okButton = FlatButton(onPressed: (){
Timer(Duration(milliseconds: 500), () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => datePicker()),
);
});
}, child: Text(' OK! '));
AlertDialog alert = AlertDialog(
title: Text('Slot unavailable'),
content: Text('This slot is already booked please select another slot'),
actions: [
okButton,
],
);
showDialog(context: context ,
builder: (BuildContext context){
return alert ;
}
);
}
The hourMinute30Interval() is nothing but a Widget that returns a timePickerSpinner which is a custom Widget. Tap here for that.
The Query that is run after passing the _eventDate and _time is in another class, and it goes as follows :
class AddingEventsUsingRajeshMethod {
getAvailableSlots(String _eventDate , String _time){
return FirebaseFirestore.instance
.collection('events')
.where('event_date', isEqualTo: _eventDate )
.where('selected_time', isEqualTo: _time)
.get();
}
}
You can name it something prettier ;)

Flutter with Firebase Google sign in: Failed assertion network_image_io.dart

I'm trying to do Firebase authentication and Google sign in using Flutter but I'm getting this error message:
'package:flutter/src/painting/_network_image_io.dart': Failed assertion: line 23 pos 14: 'url != null': is not true.
I can't understand what is wrong, can you help me?
login.dart code snippet
-> calls signInWithGoogle() method from sign_in.dart and return FirstScreen
Widget _signInButton() {
return OutlineButton(
splashColor: Colors.grey,
onPressed: () {
signInWithGoogle().whenComplete(() {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return FirstScreen();
},
),
);
});
},
sign_in.dart code snippet:
-> Here I authenticate the user and try to get logged user name, email and image
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
String name;
String email;
String imageUrl;
Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
// Checking if email and name is null
assert(user.email != null);
assert(user.displayName != null);
assert(user.photoUrl != null);
name = user.displayName;
email = user.email;
imageUrl = user.photoUrl;
// Only taking the first part of the name, i.e., First Name
if (name.contains(" ")) {
name = name.substring(0, name.indexOf(" "));
}
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
log('data: $user');
return 'signInWithGoogle succeeded: $user';
}
void signOutGoogle() async {
await googleSignIn.signOut();
print("User Sign Out");
}
FirstScreen code snippet:
-> Here I try to show user data (name, email and image) based on what I got on previously code
children: <Widget>[
CircleAvatar(
backgroundImage: NetworkImage(
imageUrl,
),
radius: 60,
backgroundColor: Colors.transparent,
),
SizedBox(height: 40),
Text(
'NAME',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black54),
),
Text(
name,
style: TextStyle(
fontSize: 25,
color: Colors.deepPurple,
fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
Text(
'EMAIL',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black54),
),
Text(
email,
style: TextStyle(
fontSize: 25,
color: Colors.deepPurple,
fontWeight: FontWeight.bold),
),
SizedBox(height: 40)
EDIT: aditional info -> console log shows "Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)"
After wasting a day to find my way around the Google SignIn package with various fixes and troubleshooting, I realised there is web package Google has made for web sign in which uses the OAuthClient verification and configuration as per their changing policy with Google Sign in.
Sadly it isn't straight forward and quick as it was before. But following the steps mentioned in the pub.dev.
https://pub.dev/packages/google_sign_in_web
Attaching my main.dart working POC snippets here.
import 'dart:async';
import 'dart:convert' show json;
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:http/http.dart' as http;
GoogleSignIn _googleSignIn = GoogleSignIn(
// Optional clientId
// clientId: '479882132969-9i9aqik3jfjd7qhci1nqf0bm2g71rm1u.apps.googleusercontent.com',
scopes: <String>[
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
void main() {
runApp(
const MaterialApp(
title: 'Google Sign In',
home: SignInDemo(),
),
);
}
class SignInDemo extends StatefulWidget {
const SignInDemo({Key? key}) : super(key: key);
#override
State createState() => SignInDemoState();
}
class SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount? _currentUser;
String _contactText = '';
#override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
setState(() {
_currentUser = account;
});
if (_currentUser != null) {
_handleGetContact(_currentUser!);
}
});
_googleSignIn.signInSilently();
}
Future<void> _handleGetContact(GoogleSignInAccount user) async {
setState(() {
_contactText = 'Loading contact info...';
});
final http.Response response = await http.get(
Uri.parse('https://people.googleapis.com/v1/people/me/connections'
'?requestMask.includeField=person.names'),
headers: await user.authHeaders,
);
if (response.statusCode != 200) {
setState(() {
_contactText = 'People API gave a ${response.statusCode} '
'response. Check logs for details.';
});
print('People API ${response.statusCode} response: ${response.body}');
return;
}
final Map<String, dynamic> data =
json.decode(response.body) as Map<String, dynamic>;
final String? namedContact = _pickFirstNamedContact(data);
setState(() {
if (namedContact != null) {
_contactText = 'I see you know $namedContact!';
} else {
_contactText = 'No contacts to display.';
}
});
}
String? _pickFirstNamedContact(Map<String, dynamic> data) {
final List<dynamic>? connections = data['connections'] as List<dynamic>?;
final Map<String, dynamic>? contact = connections?.firstWhere(
(dynamic contact) => (contact as Map<Object?, dynamic>)['names'] != null,
orElse: () => null,
) as Map<String, dynamic>?;
if (contact != null) {
final List<dynamic> names = contact['names'] as List<dynamic>;
final Map<String, dynamic>? name = names.firstWhere(
(dynamic name) =>
(name as Map<Object?, dynamic>)['displayName'] != null,
orElse: () => null,
) as Map<String, dynamic>?;
if (name != null) {
return name['displayName'] as String?;
}
}
return null;
}
Future<void> _handleSignIn() async {
try {
await _googleSignIn.signIn();
} catch (error) {
print(error);
}
}
Future<void> _handleSignOut() => _googleSignIn.disconnect();
Widget _buildBody() {
final GoogleSignInAccount? user = _currentUser;
if (user != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
ListTile(
leading: GoogleUserCircleAvatar(
identity: user,
),
title: Text(user.displayName ?? ''),
subtitle: Text(user.email),
),
const Text('Signed in successfully.'),
Text(_contactText),
ElevatedButton(
onPressed: _handleSignOut,
child: const Text('SIGN OUT'),
),
ElevatedButton(
child: const Text('REFRESH'),
onPressed: () => _handleGetContact(user),
),
],
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
const Text('You are not currently signed in.'),
ElevatedButton(
onPressed: _handleSignIn,
child: const Text('SIGN IN'),
),
],
);
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Google Sign In'),
),
body: ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: _buildBody(),
));
}
}
I found that the error I was getting was due to the lack of some OAuth Credentials settings. I found the solution here

Flutter SnackBar not showing

I have implemented a function to form submitting.I want to have SnackBar Alert to after submitted. I have tried but it doesn't work.After I added SnackBar routing also doesn't work.
addTicket() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
try{
DocumentReference ref = await db.collection('CostalLineTicketDetails').
document(ticketCato).collection("Tickets").add(
{
'startStation':startStation,
'endStation':endStation,
'price':price,
'ticketType':ticketCato,
'contactNo':contactNo,
'dateTime':dateTime,
});
setState(() => id = ref.documentID);
Navigator.push(context, new MaterialPageRoute(builder: (context) => CostalLine()));
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Ticket Added Sucessfully')));
}catch(e){
print(e);
}
}
}
}
You cannot show showSnackBar on same page after going to another screen.
You can declare _scaffoldKey and pass it to Scaffold like this
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
Scaffold(
key: _scaffoldKey,
then open snackbar like this
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(
'Welcome',
),
duration: Duration(seconds: 2),
));
Output:
Edit
You can also use flash where you don't need to pass _scaffoldKey every time.
example:
void _showBasicsFlash({
Duration? duration,
flashStyle = FlashBehavior.floating,
}) {
showFlash(
context: context,
duration: duration,
builder: (context, controller) {
return Flash(
controller: controller,
behavior: flashStyle,
position: FlashPosition.bottom,
boxShadows: kElevationToShadow[4],
horizontalDismissDirection: HorizontalDismissDirection.horizontal,
child: FlashBar(
content: Text('This is a basic flash'),
),
);
},
);
}
try this,
addTicket() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
try{
DocumentReference ref = await
db.collection('CostalLineTicketDetails').
document(ticketCato).collection("Tickets").add(
{
'startStation':startStation,
'endStation':endStation,
'price':price,
'ticketType':ticketCato,
'contactNo':contactNo,
'dateTime':dateTime,
});
setState(() => id = ref.documentID);
// Navigator.push(context, new MaterialPageRoute(builder: (context) => CostalLine()));
Scaffold.of(context).showSnackBar(SnackBar(content:
Text('Ticket Added Sucessfully')));
}catch(e){
print(e);
}
}
}
}
Define this code in any of the generalized dart file, and you can call this function at any place and will display a generic type scaffold.
import 'package:flutter/material.dart';
void showWarningSnackBar(BuildContext context, String message) {
// Find the Scaffold in the widget tree and use it to show a SnackBar.
ScaffoldFeatureController<Widget, dynamic> _scaffold;
// Find the Scaffold in the widget tree and use it to show a SnackBar.
_scaffold = Scaffold.of(context).showSnackBar(SnackBar(
content: InkWell(
onTap: () {
_scaffold.close();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
AppImage.asset(
assetName: YOUR_IMAGE_NAME,
fit: BoxFit.contain,
width: 20,
color: COLOR),
const SizedBox(
width: 10,
),
Text(
'$message',
maxLines: 2,
),
],
),
),
duration: const Duration(seconds: 10),
backgroundColor: COLOR,
));
}

Categories

Resources