user16335562
user16335562

Reputation: 5

local variable 'name' referenced before assignment in KivyMD app

I have been trying to make a list from my sqlite database . The list is append able in real time . Everything was working fine until i added an 'if' statment in order to add more list items in real time . If my database is alredy populated , code works fine even with the 'if' statment, but if my database has no values , the following error takes place

local variable 'name' referenced before assignment

here is the code

def on_start(self):
    list_item = ObjectProperty
    list_item = [] 
    self.connection = sqlite3.connect('friend_list.db')
    self.cursor = self.connection.cursor() 
    self.cursor.execute("""SELECT * FROM friend_list ;""")
    self.connection.row_factory = lambda cursor, row: row[0]
    friends = self.connection.execute('SELECT name FROM friend_list').fetchall()
    for name in friends:
        print(name)
        button = OneLineAvatarIconListItem(text = name,on_press=lambda widget:self.change_screen("Chat_Screen"))
        self.root.ids["Chat_List"].ids["list"].add_widget(button)
        button.bind(on_press=self.press)
        list_item.append(name)

    
    if name not in list_item:  #this is the condition which is causing error
        a = list_item[-1]
        button = OneLineAvatarIconListItem(text = (a),on_press=lambda widget:self.change_screen("Chat_Screen"))
        button.bind(on_press=self.press)
        self.root.ids["Chat_List"].ids["list"].add_widget(button)
        button.bind(on_press=self.press)
        print(list_item)

Upvotes: 0

Views: 154

Answers (1)

RG_RG
RG_RG

Reputation: 396

def on_start(self):
    list_item = ObjectProperty
    list_item = [] 
    self.connection = sqlite3.connect('friend_list.db')
    self.cursor = self.connection.cursor() 
    self.cursor.execute("""SELECT * FROM friend_list ;""")
    self.connection.row_factory = lambda cursor, row: row[0]
    friends = self.connection.execute('SELECT name FROM friend_list').fetchall()
    for name in friends:
        print(name)
        button = OneLineAvatarIconListItem(text = name,on_press=lambda widget:self.change_screen("Chat_Screen"))
        self.root.ids["Chat_List"].ids["list"].add_widget(button)
        button.bind(on_press=self.press)
        list_item.append(name)

    
        if name not in list_item:  #this is the condition which is causing error
            a = list_item[-1]
            button = OneLineAvatarIconListItem(text = (a),on_press=lambda widget:self.change_screen("Chat_Screen"))
            button.bind(on_press=self.press)
            self.root.ids["Chat_List"].ids["list"].add_widget(button)
            button.bind(on_press=self.press)
            print(list_item)

Missing indent in the if condition.

Upvotes: 1

Related Questions