Cannot convert value of type 'TaskElement' to expected argument type 'EnvironmentObject<TaskElement>'

2k views Asked by At

I have set my TaskElement model to be an environmentObject to be able to edit tasks. I did like this to be able to edit tasks on the same page to create a new task. Which is called NewTaskView. Btw, I also get an error in this NewTaskView_Previews but I think I can solve it myself once I can understand below two errors reason.

let contentView = ContentView().environment(\.managedObjectContext,context)
                                    .environmentObject(observer())
                                    .environmentObject(TaskElement(title: "", dateFrom: "", dateTo: "", text: ""))

But making this change turned back at me with the following errors.

struct ScrollViewTask: View {
@ObservedObject private var obser = observer()
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")
@State var shown: Bool = false
var body: some View {
    
    ScrollView(.vertical) {
        VStack {
            ForEach(self.obser.tasks) { task in
                TaskElementView(task:task)
                    .onTapGesture {
                        self.selectedTask = task
                        print(task)
                        self.shown.toggle()
                    }
            }
        }
    }
    .onAppear {
        self.obser.fetchData()
    }
    .fullScreenCover(isPresented: $shown, content: {
        NewTaskView(isShown: $shown, task: selectedTask) //Cannot convert value of type 'TaskElement' to expected argument type 'EnvironmentObject<TaskElement>'
    })
   }
 }

and

struct TaskListView: View {
@State private(set) var data = ""
@State var isSettings: Bool = false
@State var isSaved: Bool = false
@State var shown: Bool = false
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")
var body: some View {
    NavigationView {
        ZStack {
            Color(#colorLiteral(red: 0.9333333333, green: 0.9450980392, blue: 0.9882352941, alpha: 1)).edgesIgnoringSafeArea(.all)
            VStack {
                TopBar()
                HStack {
                    CustomTextField(data: $data, tFtext: "Find task", tFImage: "magnifyingglass")
                    Button(action: {
                        self.isSettings.toggle()
                    }, label: {
                        ZStack {
                            RoundedRectangle(cornerRadius: 15)
                                .frame(width: 50, height: 50, alignment: .center)
                                .foregroundColor(Color(#colorLiteral(red: 0.4274509804, green: 0.2196078431, blue: 1, alpha: 1)))
                            Image("buttonImage")
                                .resizable()
                                .frame(width: 30, height: 30, alignment: .center)
                        }
                        .padding(.horizontal, 15)
                    })
                }
                CustomSegmentedView()
                ZStack {
                    TaskFrameView()
                    VStack {
                        Spacer()
                        HStack {
                            Spacer()
                            Button( action: {
                                self.isSaved.toggle()
                            }, label: {
                                ZStack {
                                    RoundedRectangle(cornerRadius: 25)
                                        .foregroundColor(Color(#colorLiteral(red: 1, green: 0.7137254902, blue: 0.2196078431, alpha: 1)))
                                    Text("+")
                                        .foregroundColor(.white)
                                        .font(.title)
                                        .fontWeight(.bold)
                                }
                                .frame(width: 50, height: 50)
                                
                            })
                        }
                    }
                    NavigationLink(
                        destination: NewTaskView(isShown: $shown, task: selectedTask), // Cannot convert value of type 'TaskElement' to expected argument type 'EnvironmentObject<TaskElement>'
                        isActive: $shown,
                        label: {
                            Text("")
                        })
                }
            }
        }
        .navigationBarHidden(true)
        Spacer()
    }
    .navigationBarHidden(true)
  }
}

Thanks for the help in advance. If you want to see the whole project you can visit https://github.com/m3rtkoksal/TaskManager

1

There are 1 answers

1
pawello2222 On BEST ANSWER

The EnvironmentObject needs to be passed as .environmentObject to views in other environments and declared as @EnvironmentObject in a View (not as a @State)


  1. First you need to replace:
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")

with:

@EnvironmentObject var selectedTask: TaskElement

(Now you're using the same TaskElement instance in all views).

  1. Then in the same way as you pass an EnvironmentObject to the ContentView:
ContentView()
    .environmentObject(TaskElement(title: "", dateFrom: "", dateTo: "", text: ""))

you need to pass it to other environments (sheet, fullScreenCover etc.):

NewTaskView(isShown: $shown)
    .environmentObject(selectedTask)