Skip to content

How to pass a type to the initial state? #798

Answered by hecrj
H36615 asked this question in Q&A
Discussion options

You must be logged in to vote

You can provide initial state by implementing the Application trait instead and leveraging the Flags associated type:

pub fn main() -> iced::Result {
    let controller = Controller::new();

    View::run(Settings::with_flags(Flags { controller }))
}

struct View {
    controller: Controller,
}

struct Flags {
    controller: Controller,
}

impl Application for View {
    type Flags = Flags;
    // ...

    fn new(flags: Flags) -> (Self, Command<Message>) {
        (Self { controller: flags.controller }, Command::none())
    }

    fn update(&mut self, message: Message) -> Command<Message> {
        match message {
            Message::ButtonPressed => {
                self.controller.do…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@H36615
Comment options

Answer selected by H36615
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #797 on March 27, 2021 11:56.