home_widget icon indicating copy to clipboard operation
home_widget copied to clipboard

How to retrieve initial options from a SQLite database in Flutter when configuring iOS widgets?

Open disonwei opened this issue 1 year ago • 1 comments

@available(iOS 16.0, *) struct HabitEntity: AppEntity{ let id: Int let title: String

static var typeDisplayRepresentation: TypeDisplayRepresentation="widget color"
static var defaultQuery=HabitEntityQuery()

var displayRepresentation: DisplayRepresentation{
    DisplayRepresentation(title:"\(title)")
}


static let allHabits:[HabitEntity]=[
    HabitEntity(id: 1, title: "1"),
    HabitEntity(id: 2, title: "3"),
    HabitEntity(id: 3, title: "2"),
]

}

@available(iOS 16.0, *) struct HabitEntityQuery:EntityQuery{ func entities(for identifiers: [HabitEntity.ID]) async throws -> [HabitEntity] { HabitEntity.allHabits.filter{ identifiers.contains($0.id) } }

func suggestedEntities() async throws -> [HabitEntity] {

// HabitEntity.allHabits

// HabitDao.shared.getHabitList() ?? HabitEntity.allHabits;

}


func defaultResult() async -> HabitEntity? {
    try? await suggestedEntities().first
}

}

The code for my configurable widget is as above.

I want the initial value selected by the user to come from the Flutter SQLite database.

How can I retrieve this initial value and see it in the home_widget document?

I only saw how to send data from the Flutter side to the widget and receive data from the widget.

How can the widget actively request data from Flutter when the user clicks on the configuration?

disonwei avatar Jan 31 '24 11:01 disonwei

home_widget on iOS works by using an app group to share data between your app and the widget. I am not sure if an AppExtension is able to retrieve data from a database on iOS.

Without knowing your specific setup generally you should be able to do the following:

  • In Flutter get the data from your database
  • Store it using home_widget

Check the docs regarding interactivity about how to update data from the widget. I have also written an article about that here: https://medium.com/@ABausG/interactive-homescreen-widgets-with-flutter-using-home-widget-83cb0706a417

ABausG avatar Mar 24 '24 23:03 ABausG