Flutter: Create unique values from Firestore for dropdown menu - flutter

I have a dropdown menu that is being populated from Firestore with this code.
FutureBuilder<QuerySnapshot>(
future: FirebaseFirestore.instance
.collection('field_management')
.orderBy('cultivar_name')
.get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox(
height: 15.0,
width: 15.0,
child: Center(
child: CircularProgressIndicator(),
),
);
}
return DropdownButton(
onChanged: (newValue) {
setState(() {
cultivarDropdownValue =
newValue.toString();
});
},
hint: Text(cultivarDropdownValue),
items: snapshot.data!.docs
.map((DocumentSnapshot document) {
return DropdownMenuItem<String>(
value: document['cultivar_name'],
child:
Text(document['cultivar_name']),
);
}).toList(),
);
},
),
How can I create a unique list from the Firestore data to avoid duplicates in the dropdown menu?
I have tried adding items to the list in the initState() method, still received all the values from the collection (code below). Not sure what else I can try to make this work.
addDropDownItems() async {
return await FirebaseFirestore.instance
.collection('field_management')
.get()
.then((snapshot) {
for (dynamic document in snapshot.docs) {
if (!blockList.contains(document.data())) {
blockList.add(document.data());
}
}
print('block list: ${blockList}');
});
}

I stumbled across this post (How can I distinct a complex object list in DART), which solved the problem.
I made use of the darq package and just had to add this line above my print statement:
var uniqueList = blockList.distinct();

Related

Firestore get length data disappear in flutter stream

I make a chat using firebase firestore.
So. I tried express not read count in the Chat list.
But, Initially the number appears, but it changes to null data.
I don't know Why data chage null data?
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('chatRooms')
.where('emails', arrayContainsAny: [user?.email]).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
var chatLists = snapshot.data?.docs;
if (snapshot.hasError) {
return Text('Something error...');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text('is Loading...');
}
return ListView.builder(
itemCount: chatLists?.length,
itemBuilder: (context, index) {
if (chatLists?[index]['currentMsg'] != null &&
chatLists?[index]['currentMsg'] != "") {
var list = List.from(chatLists?[index]['members']);
var member = '';
if (loginUser['userName'] != null) {
for (int i = 0; i < list.length; i++) {
if (list[i] != loginUser['userName']) {
member += list[i];
}
}
}
return ListTile(
title: Row(
children: [
Text(member),
const SizedBox(
width: 20.0,
),
ChatLength(docId: chatLists![index].id, uid: user!.uid),
],
),
subtitle: SizedBox(
height: 40.0,
child: Text(
chatLists[index]['currentMsg'],
overflow: TextOverflow.ellipsis,
)),
trailing: Text(
tmFormmater(chatLists?[index]['currentTm']),
style: const TextStyle(
color: Color(0xff999999),
),
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChatScreen(
docId: chatLists![index].id,
title: member,
),
)),
);
} else {
return Container();
}
},
);
return Container();
},
),
class ChatLength extends StatelessWidget {
const ChatLength({super.key, required this.docId, required this.uid});
final String docId;
final String uid;
#override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('message_read')
.doc(docId)
.collection('message')
.where('userId', isNotEqualTo: uid)
.where('read', isEqualTo: false)
.snapshots(),
builder: (context, snapshot) {
print(snapshot.data?.size);
if (snapshot.data?.size != null) {
return Container(
child: Text('${snapshot.data?.size}'),
);
} else {
return Container();
}
},
);
}
}
===== Debug console ====
flutter: 1
flutter: null // Data changes to null immediately
message_read collection structure
message_read -> documentId -> message -> documentId -> field(userId(chat writer), read)
I'm trying get a snapshot data from firestore.
And I put the imported data into the text.
But Data changes to null immediately.
Since you use snapshots() the code is not just reading the data once, but then continues listening for changes to the data. So the two values you see mean that the snapshots() stream was triggered twice, the first time with a single message - and the second time without any messages.
My educated guess is that your code changes the read value of the document after it gets it, since it has now displayed that message to the user. But doing so means the document no longer meets the criteria of your query, so the snapshots stream gets a new event without that message in it.
Consider using a different mechanism for the query, for example I usually use a timestamp to determine what messages to show. Step by step:
Ensure each message document has a timestamp field.
For each user store (either in the database or in local storage of the app) when they last started the app.
Then request from the database the messages since they last started the app.
Make sure to start the query before you update the timestamp value, otherwise you'll never get any results.

Im trying to write code to get (doc id) from Firebase [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
Look at my database structure:
and here is my code that I want to use ID in :
Widget build(BuildContext context) {
return SafeArea(
child: InkWell(
borderRadius: BorderRadius.circular(30),
child: Dismissible(
key: UniqueKey(),
direction: DismissDirection.startToEnd,
background: Container(
color: Colors.red,
child: Row(
children: [
Icon(Icons.delete),
Text(
'Move to trash',
style: TextStyle(
color: Colors.white,
fontFamily: 'Righteous',
),
)
],
),
),
confirmDismiss: (DismissDirection direction) async {
return await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Delete Confirmation"),
content: Text("Are you sure you want to delete this item?"),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text("Delete")),
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text("Cancel"),
),
],
);
});
},
onDismissed: (DismissDirection direction) async {
if (direction == DismissDirection.startToEnd) {
print('item deleted');
}
await deleteCar(
'wam4jSgeIpWHIBLVXvmv'); //I want to get doc ID to delete it
},
There is some way:
FirebaseFirestore.instance
.collection('$YOUR_COLLECTION')
.where('uid', isEqualTo: "$UID_OF_THAT_ITEM").limit(1).get().then((value) => value.docs.first.id);
As you get it value.docs.first.id is what you need.
Not sure if I understand what you triying to achieve. But the way I see it, you can duplicate that id as an atribute of the element when you create it for example.
"aasdasd" :{
"id": "aasdasd",
"carName": "car",
}
or when you map cars, use the key you got as an atribute of your Car Model. This is an example for products.
static Future loadProducts() async {
final url = Uri.https(_baseUrl, 'products.json');
final res = await http.get(url);
final Map<String, dynamic> productsMap = json.decode(res.body);
final List<Product> products = productsMap.keys
.map((key) => Product(
id: key,
name: productsMap[key]['name'],
description: productsMap[key]['description'],
price: productsMap[key]['price'],
imagen: productsMap[key]['imagen'],
isAvailable: productsMap[key]['isAvailable'],
))
.toList();
return products;
}
'key' is the value you want.
this line solve the problem :
String gg = await FirebaseFirestore.instance
.collection('carsData')
.where('uid', isEqualTo: loggedInUser.uid)
.where('CarName', isEqualTo: nameCar)
.limit(1)
.get()
.then((value) => value.docs.first.id);
but when you have 2 items have the same CarName you must add another where() to get specific id.
FirebaseFirestore.instance
.collection('carsData')
.where('uid', isEqualTo: 'selected_car_uid')
.get()
.then((value) {
value.docs.forEach((element) {
print(element.id); // you will get your firestore id and then delete via this id.
FirebaseFirestore.instance
.collection("carsData")
.doc(element.id)
.delete()
.then((value_2) {
print('========> successfully deleted');
});
});
});
HAPPY CODING :)

Lazy Loading prevent the search or filter in my app (flutter & firestore)

I am new with flutter,
I would be grateful if you could help me with this problem:
I have a huge set of firestore data that I have to display i a listview, so I had to use Lazy Loading when I used it, the problem appeared, and I can't search or filter in all the data, only the data shown
so I need to use lazy Load or pagination and in the same time can search or filter all the firestore data
enter image description here
and
enter image description here
void initState() {
_chatScrollController = ScrollController()
..addListener(() {
if (_chatScrollController!.position.atEdge) {
if (_chatScrollController!.position.pixels == 0)
;
else {
setState(() {
loadMoreMsgs = loadMoreMsgs + a;
});
}
}
});
super.initState();
}
stream: FirebaseFirestore.instance
.collection(kProductsCollection)
.limit(loadMoreMsgs)
.snapshots(),
builder: (context, snapshots) {
return (snapshots.connectionState == ConnectionState.waiting)
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
controller: _chatScrollController,
itemCount: snapshots.data!.docs.length,
itemBuilder: (context, index) {
var data = snapshots.data!.docs[index].data()
as Map<String, dynamic>;
List<Product> products = [];
for (var doc in snapshots.data!.docs) {
products.add(
Product(
pId: doc.id,
pName: doc.get(kProductName),
pPrice: doc.get(kProductPrice),
pUrl: doc.get(kProductUrl),
pIngredients: doc.get(kProductIngredients),
pCompany: doc.get(kProductCompany),
pDose: doc.get(kProductDose),
pCode: doc.get(kProductCode),
pClass: doc.get(kProductClass),
pCountry: doc.get(kProductCountry),
pDescription: doc.get(kProductDescription),
pPregnancyCategory:
doc.get(kProductPregnancyCategory),
),
);
}
if (products[index]
.pName
.toString()
.toLowerCase()
.contains(name.toLowerCase())) {
return Padding(
padding: const EdgeInsets.fromLTRB(20, 5, 20, 5),
child: GestureDetector(
onTap: () {
Navigator.pushNamed(context, ProductDetalis.id,
arguments: products[index]);
},
child: CardWidget(
width: width, height: height, data: data)),
);
}

Flutter FutureBuilder Snapshot is null but Future Does return data

While working with Flutter for a new application client for Kanboard, I encountered the following problem. I have a FutureBuilder that should return a select dropdown menu with items but, for some reason, the Snapshot data is null, although the Future method does resolves and has data on return.
Full page.dart code here: https://pastebin.com/J48nxsdZ
The block having the problem is the following:
Widget _columnSelect() {
return FutureBuilder(
future: columnProvider.getColumns(task.projectId),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<DropdownMenuItem<String>> columnList = [];
if (snapshot.hasData) {
columnList.add(DropdownMenuItem<String>(
child: Text('Select Column'), value: 0.toString()));
_columns = snapshot.data;
} else {
columnList.add(DropdownMenuItem<String>(
child: Text('Loading..'), value: 0.toString()));
}
_columns.forEach((column) {
columnList.add(DropdownMenuItem<String>(
child: Container(
child: Text(
column.title,
),
),
value: column.id.toString()));
});
return Container(
// margin: EdgeInsets.only(left: 40.0),
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: DropdownButtonFormField(
icon: Padding(
padding: const EdgeInsets.only(right: 12),
child: Icon(Icons.view_column, color: Colors.blue),
),
items: columnList,
value: _columnId,
decoration: InputDecoration(helperText: 'Optional'),
onChanged: (newValue) {
_columnId = newValue;
},
),
);
},
);
}
This is a duplicate of a widget in the same form for a user dropdown select. The original widget (in the same page) is the following:
Widget _ownerSelect() {
return FutureBuilder(
future: userProvider.getUsers(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<DropdownMenuItem<String>> usernameList = [];
if (snapshot.hasData) {
usernameList.add(DropdownMenuItem<String>(
child: Text('Select Owner'), value: 0.toString()));
_users = snapshot.data;
} else {
usernameList.add(DropdownMenuItem<String>(
child: Text('Loading..'), value: 0.toString()));
}
_users.forEach((user) {
usernameList.add(DropdownMenuItem<String>(
child: Container(
child: Text(
user.name,
),
),
value: user.id.toString()));
});
return Container(
// margin: EdgeInsets.only(left: 40.0),
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: DropdownButtonFormField(
icon: Padding(
padding: const EdgeInsets.only(right: 12),
child: Icon(Icons.person, color: Colors.blue),
),
items: usernameList,
value: _ownerId,
decoration: InputDecoration(helperText: 'Optional'),
onChanged: (newValue) {
_ownerId = newValue;
},
),
);
},
);
}
For some reason, the "_columnSelect" AsyncSnapshot is null always, even when the Future method is working fine:
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:kanboard/src/models/column_model.dart';
import 'package:kanboard/src/preferences/user_preferences.dart';
class ColumnProvider {
final _prefs = new UserPreferences();
Future<List<ColumnModel>> getColumns(projectId) async {
final Map<String, dynamic> parameters = {
"jsonrpc": "2.0",
"method": "getColumns",
"id": 887036325,
"params": {"project_id": projectId}
};
final credentials = "${_prefs.username}:${_prefs.password}";
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials);
final resp = await http.post(
Uri.parse(_prefs.endpoint),
headers: <String, String>{"Authorization": "Basic $encoded"},
body: json.encode(parameters),
);
final decodedData = json.decode(utf8.decode(resp.bodyBytes));
final List<ColumnModel> columns = [];
final List<dynamic> results = decodedData['result'];
if (decodedData == null) return [];
results.forEach((column) {
final columnTemp = ColumnModel.fromJson(column);
columns.add(columnTemp);
});
print(columns);
return columns;
}
}
The output of "print(columns)" returns:
I/flutter ( 9486): [Instance of 'ColumnModel', Instance of 'ColumnModel', Instance of 'ColumnModel', Instance of 'ColumnModel']
I don't know what I'm missing here. The form has 2 users dropdown select (with the original FutureBuilder Widget) which works just fine. The Column widget with the Future Builder is the one with the "null" problem in snapshot.data.
Thank you in advance for your time and support with this!
I just found where the problem was:
In the form page(new Task page), The columnProvider.getColumns(task.projectId)) wasn't executing because the "task.projectId" parameter is a String, but the API needs an int.
I was confused because the method were being called by the previous page (A project Page with all the tasks) and the getColumn's argument was indeed an integer: int.parse(projectId).
The Kanboard API doesn't return an error code if the ID parameter is other than INT with this specific call "getColumns" (for some reason).
Of course, Flutter (or Dart) is waiting for a response from http.post that would never arrive. When comparing the two calls from the two pages, I noticed the difference.
So, in conclusion, I specified the int data type argument in the getColumn definition in order to avoid any confusion:
Future<List<ColumnModel>> getColumns(int projectId) async {
Best Regards!

How to apply an async operation on list view builder?

I am trying to get set of encrypted data documents from firebase and display them on a list view on flutter.
I used a stream builder for obtaining data and started displaying it on the list view. But I cant perform the decryption operation on each data item as it is an async operation. What is the best way to do this?
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection(ScopedModel.of<User>(context).userId)
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: new Container(
child: CircularProgressIndicator(),
));
default:
if (snapshot.data.documents.length == 0) {
return Container(
padding: EdgeInsets.all(16.0),
child: Row(
children: <Widget>[
Text('Empty',),
],
),
);
}
final docs = snapshot.data.documents;
return ScrollConfiguration(
behavior: ScrollBehavior(),
child: ListView.builder(
itemCount: len,
scrollDirection: Axis.horizontal,
itemBuilder: (context, position) {
// Where should I decrypt the below data?
// let decrypted = await myDecryptionFunction(docs[position]['myDataKey']) ;
// the above is not working
// this will show the encrypted text
return Text(docs[position]['myDataKey']);
}
....
For your situation you can use a StreamController with a helper class to hold the information.
The following is just an example but adapt it to your own needs.
// Helper classes
// adapt it to your own business case
class Notes {
String title;
String description;
Notes({this.title, this.description});
}
class NotesFromDb {
String connectionState;
bool hasError;
String error;
List<Notes> notes;
NotesFromDb({this.connectionState, this.hasError, this.error, this.notes});
}
// The Streambuilder
StreamBuilder<NotesFromDb>(
stream: sController.stream,
builder: (BuildContext context, AsyncSnapshot<NotesFromDb> snapshot) {
// Here you can check for errors
if (snapshot.data.hasError == true) {
return Container(
color: Colors.red,
child: Text(snapshot.data.error),
);
}
// Here you can check for your connection state
if (snapshot.data.connectionState == 'Loading') {
return Container(
color: Colors.yellow,
child: CircularProgressIndicator(),
);
}
// Here you can show your data
var info = snapshot.data.notes.map((doc) {
return Text(doc.title);
});
return Center(
child: Container(
color: Colors.deepOrange,
child: Column(
children: info.toList(),
),
));
})
// Here is how you can handle the decrypt data
// using a FloatingActionButton for loading the data (for example)
FloatingActionButton(
onPressed: () async { // you would need to add the async
List<Notes> theNotes; //just to hold the information
// Use this to allow to show the CircularProgressIndicator
sController.sink.add(NotesFromDb(connectionState: 'Loading'));
var snapshots = Firestore.instance.collection('notes').snapshots();
snapshots.listen((QuerySnapshot data) {
theNotes = data.documents.map((DocumentSnapshot doc) {
// Build your data
return Notes(
title: doc.data['title'],
description: doc.data['description']);
}).toList();
}, onError: (err, stack) {
// If an error happend then send the error to the stream
sController.sink
.add(NotesFromDb(hasError: true, error: err.error));
});
// Here you can to decrypt the documents with your function
var decryptDocuments = await Future.delayed(Duration(seconds: 2)); //Whatever future function
// Once you have the decrypt documents, you would need to send that to the stream.
// set the connectionState to Done, so the spinner is not showed anymore.
sController.sink.add(NotesFromDb(
hasError: false, connectionState: 'Done', notes: decryptDocuments));
},
child: Icon(Icons.arrow_forward),
)
Take the example just for illustration on how to do it.
Hope this help.