Drift Flutter - Get Specific Field Value From Table - flutter

I am using Drift for the first time and I have been able to create the tables in the database and insert into a table in the database. Now I want to show a field value from the table on my app, for example, a table called Employee that has the employee name in the "name" field, I want to be able to display the employee's name at the top right of the app when they log in. So I want to get the value of the name field for that employee.
How can I achieve this?
A lot of examples I see are for displaying Lists and that's not really what I'm looking for.
I tried to do a get on the table but I don't know how to get the snapshot of the table's data.
Future<List<Employee>> getEmployee() async {
return await select(employee).get();
}
Please how can I achieve this?

To get a value from Future you must use await
for more details pls have alook in the official documents
So You can get employees list like this
void getEmployees() async {
final employeesList = await select(employee).get();
}
Where employee is the table name.

Related

How can I retrieve all row of one column in firebase flutter

I am trying to make a firebase database call to retrieve all row of one particular column in the firebase database of my app and display it in a textbox in my flutter app.
That is, to retrieve all phone numbers in the Users table.
For instance, in SQL we can do like this:
SELECT (PhoneNumers) FROM Users
I am new in flutter/dart, I need some help.
Its really easy.
First of all, setup and initialize firebase in you project. Then make a function and create an instance of firestore like this
FirebaseFirestore firestore = FirebaseFirestore.instance;
Then make something called Collection reference:
CollectionReference users = await firestore.collection("users").get().then((querySnapshot) {
querySnapshot.docs.forEach((doc) {
print(doc["PhoneNumber"].toString())
});
This will grab every collection from Users, loop over them and then print out the document PhoneNumber to console. There is no way to just get a single document field from firestore. It might seem weird, but its quite useful at times. Right now, you have access to every document field of every user of your app.

trigger on opportunity whenever the data of the field get changed in the Custommetadata, then update that field data on the related Account field

2 Fields on Custom metaData:
Opportunity Field Name
Account Field Name
trigger on opportunity whenever the data of the field get changed (the field that are mentioned in the Custom metadata), then update that field data on the corresponding Account field.
I tried to get Field Values from Custom metaData like Map<Id,Object_Field_Mapping__mdt> metaData = new Map<Id,Object_Field_Mapping__mdt>([SELECT Account_Field_Name__c,Opportunity_Field_Name__c FROM Object_Field_Mapping__mdt]);
And now the problem is I am not able to compare these value with the whole Account object so that i can update on Opportunity.....This sounds little bit confusing but this is what i have to do
Is there any way to compare CustomMetaData Field Value with Account Object..
Someone told me it can be used by Schema/sObjects but I am not sure how
You can use Schema like this
Map<String, Schema.SObjectField> accFields = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap();
and you can access its field value like this accFields.values()
for(Schema.SObjectField field : accFields.values())
{
//Some Code here
}
for more information about Schema Class and it's Method please check Schema Class
Hope it Helps
Thanks

How to store markers (with id) and populate map for each user using mongodb?

I'm a complete noob so bear with me please -
Currently users can add markers and my plan is to use the ondraw event to get the marker lat/lng and store that in mongodb, however i don't know how to get the individual markerID - ._leaflet_id returns same id for every marker.
But if i generate id for the marker using something like Date.now() how can i fetch that id from db if a user clicks on a marker? Each marker needs to be unique since each one will have a different redirect for onclick().
Thank you
I had a similar issue sometime back. What I did was create a table with [unique key id] and another column as markerJson,
var marker = new L.Marker([0, 0]);
// Get the GeoJSON object
var geojson = marker.toGeoJSON();
which has the following
{
"type":"Feature",
**"properties":{},**
"geometry":{
"type":"Point",
"coordinates":[0,0]
}
}
Save the above json in markerjson Column created above.
You can see in the json above the properties feature "properties":{}, does the logic
from db fetch and append the [unique key id] to properties "properties":{} e.g
"properties":{unique-key_id:28}, now user can access the unique id from properties.
But in the case above on creating an event, the create event has properties{} feature, just append _leaflet_id to properties with the generated Id with Date.now().

How to select a column from table/collection using odata URI?

I am using mongodb and odata.
I want to select name field alone for particular user id. (i.e) select name from userdata where userid=1;
/*my collection schema - userdata*/
{
id:number,
userid:string,
name:string,
data:object
}
I tried http://localhost:27017/userdata?$format=json&$filter=userid eq '1'&$select=name
Instead of getting name file alone I got whole object/document that matches userid=1. What I am doing wrong here?
I spoted the problem after the comment from #jps.
The issue is not with query, the problem is with data model which I am using is mismatching with my database schema (i.e) I missed out name field in model, so it is returning whole collection.
Now model is fixed, so the service is responding back with names for given userid.

How to programmatically refresh a screen from onFinished callback function?

e.g. we have this one guy calling server function for creating a new entity:
function loadData() {
var vServerController = mobileController.serverController();
var vJSONRequest = vServerController.createJSONRPCRequest();
vJSONRequest.setQueryMethod("createSomeNewElementBasedOnTwoIds");
vJSONRequest.addParameter("firstID", 1);
vJSONRequest.addParameter("secondID", 2);
vJSONRequest.setOnFinish(callBackOnFinish);
vServerController.addToQueue(vJSONRequest);
}
function callBackOnFinish() {
var vController = mobileController.activeController();
vController.showView(Screens.SomeScreen, true);
}
So how we can refresh a Screen after this call? The server will return not a full set of data but just a new one.
What is the best approach to doing this?
Assuming you want to update the currently displayed data, you have two options - and you propably don't need to use a callback for this
Replace the displayed dataset
Update the displayed record
(if you had user input or created a new entry) merge the entries
1: just return the data from the server with foundset.setDataMode(DataMode.REPLACE) - it will automatically refresh the displayed data. But In case you are working with multiple records for one entity and you only want to update one of them, use option 2.
2: Return only the specific record you want to update without datamode replace. The record is identified by it's key/id elements. You can find those marked with a little key symbol in the entity editor. So just make sure that those key elments and any additionaly elements you want are returned. The AppConKit will automatically merge the existing record with the data and display that.
3: If you created a new record on the device, that record will have a value called client_uuid. If you now return a record from the server that contains both the client_uuid AND the key element, the record created on the device will be merged with the server created record and the new record will displayed
Hope this helps!