Problem with Firebase and ListView – get value for key


Hello everyone,

I’m a young student with little real experience in Kodular, and I’m having an issue with Firebase Realtime Database and displaying data in a ListView.

I have a global variable missionsDict that contains all my data.

I want to loop through each key (unique ID) to get each mission dictionary.

Then, I want to display the title and description in a ListView.

I’m using the loop:

for each key in dictionary keys of global missionsDict:
get value for key key
in dictionary global missionsDict
to oneMission

Then for the title and description:

get value for key “title” in dictionary oneMission to tempTitle
get value for key “description” in dictionary oneMission to tempDesc

Problem: Even with this, I cannot populate my list, and the values remain empty in the ListView.

Hi,

During a for each key in dictionary loop, in each cycle the variable key will hold the key from your dictionary, and value will hold the value of that key.
Example

{
	"missions": {
		"item1": {
			"title": "title1",
			"description": "description1"
		},
		"item2": {
			"title": "title2",
			"description": "description2"
		},
		"item3": {
			"title": "title3",
			"description": "description3"
		}
	}
}

By looping through the missions_dictionary, the first cycle will have item1 as the key, while the value will contain

{
	"title": "title1",
	"description": "description1"
}

So, to solve your problem, you just need to modify the for each and two small things.

1 Like

“It’s about storing data in Firebase and displaying it in a ListView using Kodular. I’m trying to loop through dictionaries and retrieve their values, but I can’t get the ListView to show the data correctly. Instead, I receive this error message.”

Show the blocks you are using related to this for each.

Thanks for your help! Just to clarify: I use the make dictionary block when I create and send data to Firebase (for example in Button.Click), so that the data is stored as a dictionary. But when I read the data back with Got Value, I don’t use make dictionary because Firebase already returns it as a dictionary.

The problem is that even with this, the data is not stored correctly or it doesn’t display in the ListView.

To save data in Firebase in dictionary format, the best way is to use the Web component.
Read this recent post that talks about it and check :raising_hands:Tim:raising_hands: ’s guide, which covers everything you need to konw.

1 Like

The goal was to create an application that allows a user to add information on one page, and then have that information displayed in a list on another page. I’m using Firebase, but despite your help, I’m still facing difficulties.

It would really help if you provided a screenshot your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select “Download Blocks as Image”. You might want to use an image editor to crop etc. if required. Then post it here in the community.

Taifun


From the documentation Web - Kodular Docs

JSON Text Decode

Decodes the given JSON encoded value to produce a corresponding AppInventor value A JSON list [x, y, z] decodes to a list (x y z), A JSON object with name A and value B,(denoted as A:B enclosed in curly braces) decodes to a list((A B)), that is, a list containing the two-element list (A B).

Which means, mision_dic is not a dictionary
Also always fill all sockets

Taifun

Let’s see if this project helps you,

Let’s suppose we have a database structured like this.

I’ve created separate post and get functions for convenience


Post



post function takes just one parameter, a dictionary, and will create a node under missions with a unique ID as the key (which also makes data management easier)
immagine


Get

blocks(12)


The get function doesn’t need any parameters because it points directly to missionsProject/missions.


Web.GotText


Nothing complicated here,

if responseCode = 200 (success) 
   it the function = "post", 
       display a message with the response, 
       clear the textboxes,
       reset the function variable, 
       and call get function to retrieve the updated list
   else if function = "get"
       loops through the dictionary returned as the response
       for each value in dictionary (just like you did before),
       performs a join (title | description) + add to missionsList
       and displays it in the ListView.
If the responseCode is something else, 
    just display a message with infos.

Result

SimplePostGetFB.aia (5.0 KB)

1 Like

Well, why dont you think like this?

Here i stored the values in fb as dic method only as you did in the first post.

and this is my fb structure.

1 Like

Hello,
I managed to display the list of missions in a ListView (thanks to your previous advice :folded_hands:).

Now, I’m trying to do the same to display the list of applicants for a mission.
The problem is that as soon as I try to retrieve and display the applicants, the app crashes immediately, even if there is nothing in Firebase yet.

:backhand_index_pointing_right: Does anyone know why a ListView can crash an app when trying to display data coming from Firebase?
Is there a specific check or process to do before adding the data to the ListView?

Thanks in advance for your help :folded_hands:

Hi,

If you do a get on a node that doesn’t exist it returns a null string, so you would just need to add an exception in Web.GotText

if response code = 200
   if function = get
      if responseContent <> null
         then execute the code
1 Like

Hi,

I have a ListView displaying missions with title and description from Firebase. Each mission has a unique ID.

I want that when a mission is clicked, its ID is retrieved and sent to the Details screen to manage the applicants.

How can I ensure that each mission keeps its correct ID when passing it to the other screen?

Thanks for your help!

Hi,
Last time I replied I hadn’t looked carefully at the image you posted.
I suggest making two changes if you haven’t already,

  • write the Post function in lowercase,
  • and place the set ListView.Elements outside the loop.

Regarding your request, I’d suggest comparing the position of the clicked index with the position of the item in the dictionary keys.

Maybe with the blocks it’s easier to understand.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.